问题
In our chat app we want to use cool new Library SQLBrite to update chat on database changes. Since our chat has endless scrolling, and chat room can have very big list of messages, we want to split ArrayList supplied to Chat ListView adapter into two lists. Check graphic for the idea.
- We want to set point in database above which, old messages will be queried by normal SQLite queries. And below that point we want set SQLBrite, that will bring us fresh messages added to database.
- Each part should populate its corresponding ArrayList. And two arrayLists should be combined in one adapter.
My question is it possible to do? If yes how we can combine and handle two dynamic ArrayLists in single adapter?
Edit 1 1. I need to keep chat scroll position during from resetting, and no flickers during ArrayLists update.
回答1:
1.With the help of generics you can handle two arraylist with single ArrayList.
For example in adapter :
setListData(ArrayList<T> pListData)
{
mListData=pListData;
}
In View
getView(int position, View convertView, ViewGroup parent){
T commonModel= getItem(position);
if(T instanceof ArrayListOneModel){
ArrayListOneModel model1=(ArrayListOneModel)T;
do stuf for first arraylit...
}
}
- If you are using same model you can set a type (enum ) for both arraylist & during showing time you can check that.
3.Otherwise you can first add old data in arraylist & then using collection addAll() add 2nd latest message list in it. then
adapter.notifyDataSetChanged()
will set first old message then will set latest message in your list
More Clarification: In second approach if you have different models for both arraylist then contain an enum in both model as a setter getter.
public enum eType{
FIRST_LIST_TYPE,SECOND_LIST_TYPE
}
During Fetching data from different DB's set Type in model. e.g
public class model{
private enum eType;
// other setter getter value from your DB /** * Setter getter: */
public void seteType(enum eType)
{
this.eType = eType;
}
public enum geteType()
{
return eType;
}
}
During fetching data set Type e.g.
Model model = new Model();
model.seteType(eType.FIRST_LIST_TYPE) ;
//same for 2nd db.
& simply check type inside getView() according to your requirement.
回答2:
yes that is possible inside BaseAdapter getCount method write following code
@Override
public int getItemCount() {
return list1.size()+list2.size();
}
and inside getView method you can do something like below
public View getView(int position, View convertView, ViewGroup viewGroup) {
if(position < list1.size) {
Object object = list1.get(position);
//write code to inflate view here related to list 1
}
else {
Object object = list2.get(position - list1.size());
//write code to inflate raw here related to list 2
}
}
回答3:
You can pass only one list in adopter, which means you have to merge both arrays. In order to merge both array. they have to be of same type, i.e. Array of same custom object.
If arrays are updating dynamically, then merge arrays again, as their data changes, and call notifyDataSetChanged()
each time, to reflect changes in listview
回答4:
Yes you can do it. but both arraylist should have common data format. for eg .. In adapter you can make method like
public void addMessages( <your_array_list> data ) {
list.addAll(data); //where list is your data container
}
now you may have two arraylist like
ArrayList<your_type> oldMsg;
ArrayList<your_type> newMsg;
..
..
...
.
.
so you can call adapter method which we have created
yourAdapter.addMessages(oldMsg);
yourAdapter.addMessages(newMsg);
来源:https://stackoverflow.com/questions/29788684/android-listview-adapter-with-two-arraylists