为什么我的列表视图适配器不显示在新的观点正确的布局? [英] Why my listview adapter doesn't show the right layout on new view?

查看:178
本文介绍了为什么我的列表视图适配器不显示在新的观点正确的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不基于android系统的MMS和SMS数据库的自定义光标适配器。 code看起来像这样如下:

  @覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){    消息m = Message.getMessage(背景下,光标); //从游标得到消息
    INT T = m.getType(); //这得到它。2 = recv的,1 =发送信息的类型    开关(T){
    案例Message.MMS_IN:// 128
        返回mInflater.inflate(R.layout.messagelist_item_recv,父母,假);
    案例Message.MMS_OUT:// 132
        返回mInflater.inflate(R.layout.messagelist_item_sent,父母,假);
    案例Message.SMS_IN:// 2
        返回mInflater.inflate(R.layout.messagelist_item_recv,父母,假);
    案例Message.SMS_OUT:// 1
        返回mInflater.inflate(R.layout.messagelist_item_sent,父母,假);
    默认:
        返回null;
    }
}

R.layout.messagelist_item_sent 是发送消息和 R.layout.messagelist_item_recv 是收到的消息。
但看我的短信,当列表视图正确显示,但是当我去了列表视图新的意见,布局混在一起被首先显示的行。该recv的布局,其中发送布局应该是,反之亦然。任何一个知道为什么这样的问题会出现?

*编辑**

  @覆盖
公众诠释getItemViewType(INT位置){
     //光标移动到所述位置
光标C =(光标)的getItem(位置);
消息m = Message.getMessage(背景下,C);如果(isInbox(m.getType())){
   inflater.inflate(recv的观点);
 //它被缩短
}其他{
   inflater.inflate(发送视图);
//并确定正确的类型排布置的
     //返回0或1
     //使用code,你目前有来自NewView的方法
}


解决方案

  

但看我的短信,首先显示的行,当
  列表视图正确显示,但是当我去了列表视图新
  意见,布局混淆。


这很可能发生,因为你不处理的的ListView 回收机制的权利。在 NewView的方法不要求每一行,当没有一个循环视图用于行布局(如它被称为当的ListView 首先显示,这就是为什么行出现在正确的顺序)。否则,该的ListView 将使用循环视图其可以是或可以不是观正确类型

您应该使用方法 getViewTypeCount() getItemViewType()。该方法 getViewTypeCount()将返回两个(因为你有两种类型的行?!?),然后执行,以确定正确的行类型的逻辑 getItemViewType

  @覆盖
公众诠释getViewTypeCount(){
    返回2;
}@覆盖
公众诠释getItemViewType(INT位置){
     光标C =(光标)的getItem(位置);
     消息m = Message.getMessage(背景下,C);
     开关(m.getType()){
         案例Message.MMS_IN:// 128
             返回1;
         案例Message.MMS_OUT:// 132
             返回0;
         案例Message.SMS_IN:// 2
             返回1;
         案例Message.SMS_OUT:// 1
             返回0;
         默认:
             返回0;
      }
}

然后在 NewView的方法:

  @覆盖
公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
    INT ROWTYPE = getItemViewType(cursor.getPosition());
    如果(ROWTYPE == 0){
        返回mInflater.inflate(R.layout.messagelist_item_sent,父母,假);
    }否则如果(ROWTYPE == 1){
        返回mInflater.inflate(R.layout.messagelist_item_recv,父母,假);
    }其他{
        返回null;
    }
}

I have a custom Cursor Adapter that is based off of the MMS and SMS database in android. code looks like this as follows:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    Message m = Message.getMessage(context, cursor); // gets message from cursor
    int t = m.getType(); // this gets the type of message it is .. 2 = recv, 1 = sent

    switch(t){
    case Message.MMS_IN: // 128
        return mInflater.inflate(R.layout.messagelist_item_recv, parent, false);
    case Message.MMS_OUT: // 132
        return mInflater.inflate(R.layout.messagelist_item_sent, parent, false);
    case Message.SMS_IN: // 2
        return mInflater.inflate(R.layout.messagelist_item_recv, parent, false);
    case Message.SMS_OUT: // 1
        return mInflater.inflate(R.layout.messagelist_item_sent, parent, false);
    default:
        return null;
    }
}

The R.layout.messagelist_item_sent is for sent messages and the R.layout.messagelist_item_recv is for received messages. But looking at my messages, the rows that are first shown when the listview are shown correctly, but when I go up the listview to new views, the layouts are mixed up. The recv layout is where the sent layout is supposed to be and vice versa. Any one know why this kind of problem would be occuring?

* EDIT **

@Override
public int getItemViewType(int position) {
     // move the cursor to the position
Cursor c = (Cursor)getItem(position);
Message m = Message.getMessage(context, c);

if (isInbox(m.getType())){
   inflater.inflate(recv view);
 // it's been shortened 
} else {
   inflater.inflate(send view);   
// and determine the correct type of row layout
     // return 0 or 1
     // use the code that you currently have from the newView method
}

解决方案

But looking at my messages, the rows that are first shown when the listview are shown correctly, but when I go up the listview to new views, the layouts are mixed up.

That is probably happening because you don't handle the ListView recycling mechanism right. The newView method is not called for each row, it's called when there isn't a recycled view to be used for the row layout(like when the ListView is first shown, that's why the rows appear in the correct order). Otherwise, the ListView will use a recycled view which may or may not be the right type of view.

You should use the methods getViewTypeCount() and getItemViewType(). The method getViewTypeCount() will return two(as you have two types of rows?!?) and then implement the logic to determine the correct row type in the getItemViewType:

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
     Cursor c = (Cursor)getItem(position);
     Message m = Message.getMessage(context, c);
     switch(m.getType()){
         case Message.MMS_IN: // 128
             return 1;
         case Message.MMS_OUT: // 132
             return 0;
         case Message.SMS_IN: // 2
             return 1;
         case Message.SMS_OUT: // 1
             return 0;
         default:
             return 0;
      }
}

Then in the newView method:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    int rowType = getItemViewType(cursor.getPosition());
    if (rowType == 0){
        return mInflater.inflate(R.layout.messagelist_item_sent, parent, false);
    } else if (rowType == 1){    
        return mInflater.inflate(R.layout.messagelist_item_recv, parent, false);
    } else {
        return null;
    }
}

这篇关于为什么我的列表视图适配器不显示在新的观点正确的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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