具有两个ArrayList的Android ListView适配器 [英] Android ListView adapter with two ArrayLists

查看:319
本文介绍了具有两个ArrayList的Android ListView适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的聊天应用程序中,我们想使用很酷的新库 SQLBrite 来更新数据库更改时的聊天记录.由于我们的聊天无休止地滚动,并且聊天室可以包含很大的消息列表,因此我们希望将提供给Chat ListView适配器的ArrayList分成两个列表.查看图形中的想法.

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.

  1. 我们要在数据库中设置一个点,在该点之上,常规SQLite查询将查询旧消息.在此之后,我们需要设置SQLBrite,它将为我们带来新消息添加到数据库中.
  2. 每个部分都应填充其对应的ArrayList.并且两个arrayLists应该组合在一个适配器中.

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

My question is it possible to do? If yes how we can combine and handle two dynamic ArrayLists in single adapter?

编辑1 1.我需要在重置过程中保持聊天滚动位置,并且在ArrayLists更新期间不会闪烁.

Edit 1 1. I need to keep chat scroll position during from resetting, and no flickers during ArrayLists update.

推荐答案

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

1.With the help of generics you can handle two arraylist with single 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. 如果使用相同的模型,则可以为两个arraylist设置类型(枚举) &在显示期间,您可以检查一下.
  1. If you are using same model you can set a type (enum ) for both arraylist & during showing time you can check that.

3.否则,您可以先将旧数据添加到arraylist&然后使用集合 addAll()在其中添加第二个最新消息列表.然后

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

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

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
   }

在从不同DB的集合中提取数据的过程中. 例如

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.

这篇关于具有两个ArrayList的Android ListView适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆