Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 459
Rafael Avatar asked Apr 22 '15 05:04

Rafael


People also ask

How to connect an adapter with ListView in Android?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

How to set adapter for ListView?

Android App Development for Beginners This example demonstrates how do I add custom adapter for my listView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is ArrayAdapter in Android?

ArrayAdapter is the most commonly used adapter in android. When you have a list of single type items which are stored in an array you can use ArrayAdapter. Likewise, if you have a list of phone numbers, names, or cities. ArrayAdapter has a layout with a single TextView.


2 Answers

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.
like image 142
Deepanker Chaudhary Avatar answered Sep 23 '22 20:09

Deepanker Chaudhary


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
       } 
 }
like image 45
amodkanthe Avatar answered Sep 23 '22 20:09

amodkanthe