Search code examples
androidlistviewadaptersqlbrite

Android ListView adapter with two ArrayLists


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.


Solution

  • 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.