Android ListView adapter with two ArrayLists

耗尽温柔 提交于 2019-12-05 10:50:55

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

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

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