Android ListView adapter with two ArrayLists
Asked Answered
W

4

5

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. enter image description here

  1. 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.
  2. 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.

Wyman answered 22/4, 2015 at 5:43 Comment(2)
Adopter will accept only one array, you have to merge both arrays. You have to find a solution for updating lists dynamically and re-mergingBadajoz
why not using a MergeCursor?Samaniego
K
5

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...
      }
 }
  1. 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.
Kev answered 22/4, 2015 at 5:55 Comment(3)
@Deepanker Could you explain little more about second solution with code snippetAurie
@Aurie I have added more clarification for 2nd approach. But 3rd approach is best no need to check any type only addAll will add all data.Kev
Thanks @DeepankerChaudhary. I have used add all method earlier. Currently I am using 2nd approach using Boolean to differentiate list type, but this way, app will not be scale if there are more list type. So wanted to explore ways using enum other than using generics.Aurie
M
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
       } 
 }
Mel answered 22/4, 2015 at 5:55 Comment(0)
F
1

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);
Fashion answered 22/4, 2015 at 5:57 Comment(1)
don't forget to write notifyDataSetChanged()Fashion
B
1

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

Badajoz answered 22/4, 2015 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.