how to make android recycler views adapter accept different data types?

╄→гoц情女王★ 提交于 2019-12-12 03:38:00

问题


i have a lot of recycler views, they all use the same adapter, i used to update them with arraylists of objects that id make as i went like this

ADAPTER

private List<CardWriter> cardMakerList;


public CardAdapter(List<CardWriter> cardMakerList){
    this.cardMakerList = cardMakerList;
   // this.mContext = context;
}

ACTIVITY

 public List<CardWriter> cardMakerList = new ArrayList<>();
 public CardAdapter cardAdapter;
 recyclerView.setAdapter(cardAdapter);
 CardWriter cardWriter = new 
 CardWriter(getResources().getDrawable(R.drawable.happy),"HAPPY","happy ");
 cardMakerList.add(cardWriter);

THE PROBLEM

Ive now switched to using databases for each recycler view (using greenDao see this question) so my adapter now uses the below code (addNewCard refers to the generated java class made by greendao)

 public List<addNewCard> leaseList = new ArrayList<>();

but now to get my second database(2) its the exact same properties but greendao makes a different file so i should use this in my adapter

 public List<addSimpleCard> leaseList = new ArrayList<>();

but this would mean a new adapter for every database and im going to need a lot of them. All the databases im making are just lots of the same object with different properties (an image and 2 strings) but each database needs to be managed individually. my last question (in the link above) asks how i can make all the same databases with a different title which i think would also solve my problem.

WHAT IVE TRIED I've tried just creating an static reference to the array list and passing it the new one eg:

public static CardAdapterDB cardAdapterDB;

cardAdapterDB = new CardAdapterDB(leaseList);

this works but trying to change it for a new database

cardAdapterDB = new CardAdapterDB(simpleleaseList);

gives me an error

CardAdapterDB(java.util.List<greendao.addNewCard>) in CardAdapterDB cannot be applied to (java.util.List<greendao.addSimpleCard>)

addSimpleCard is the name of my other java file generated by greendao so this makes perfect sense but how can i get around this without creating loads of new adapters and switching them all the time?

since asking this ive tried multiple ways to get this to work but have yet to find something suitable

来源:https://stackoverflow.com/questions/37979893/how-to-make-android-recycler-views-adapter-accept-different-data-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!