带有两个 ArrayList 的 Android ListView 适配器

2024-02-04

In our chat app we want to use cool new Library SQLBrite https://github.com/square/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. 我们希望在数据库中设置一个点,高于该点,旧消息将通过普通 SQLite 查询进行查询。在这一点之下,我们需要设置 SQLBrite,这将为我们带来添加到数据库的新消息。
  2. 每个部分都应填充其相应的 ArrayList。两个 arrayList 应该合并在一个适配器中。

我的问题是可以吗?如果是的话,我们如何在单个适配器中组合和处理两个动态 ArrayList?

Edit 11.我需要在重置期间保持聊天滚动位置,并且在ArrayLists更新期间不闪烁。


1.借助泛型,您可以使用单个ArrayList处理两个ArrayList。

例如在适配器中:

setListData(ArrayList<T> pListData)
{
    mListData=pListData;
}

在视野中

 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. 如果您使用相同的模型,您可以为两个数组列表设置类型(枚举) &在放映期间您可以检查这一点。

3.否则你可以先在arraylist中添加旧数据,然后使用collectionaddAll()在其中添加第二个最新消息列表。然后

  adapter.notifyDataSetChanged() 

将设置第一条旧消息,然后设置列表中的最新消息

更多说明:在第二种方法中,如果两个 arraylist 有不同的模型,则在两个模型中都包含一个枚举作为 setter getter。

  public enum eType{
   FIRST_LIST_TYPE,SECOND_LIST_TYPE
   }

在从不同数据库的集合中获取数据期间,输入模型。 例如

  public class model{
   private enum eType;

// 来自数据库的其他 setter getter 值 /** * 设置器获取器: */

   public void seteType(enum eType)
     {
      this.eType = eType; 
     }
    public enum geteType()
   {
       return eType;
    }

  }

在获取数据集类型期间,例如

 Model model = new Model();
 model.seteType(eType.FIRST_LIST_TYPE) ;
  //same for 2nd db.
 & simply check type inside getView() according to your requirement.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有两个 ArrayList 的 Android ListView 适配器 的相关文章