【问题标题】:problem in inflating two layout in one listview using custom adapter使用自定义适配器在一个列表视图中膨胀两个布局的问题
【发布时间】:2011-08-11 16:00:32
【问题描述】:

我在一个列表视图中膨胀了两个布局。数据为json格式。我所做的是将 json 连接在一起,并检查每个布局的适当位置(第一个布局仅出现从位置 0 到 first-json 的长度 - 1)。

当列表视图变长时会出现问题,我可以向下滚动浏览它。它出现了一个空指针异常。所以我注释了一些代码,错误消失了。但我得到的并不是我所期待的: 它随机交换格式。 (假设第一个json的长度是x,所以0到x-1应该是第一个布局。但是当我上下滚动时,有时0和x之间的某个地方变成了第二个布局。以及其他行> x,有时会更改为第一个布局)

这是适配器的代码

public class CustomAdapter extends BaseAdapter {

private Activity activity;
private JSONArray data;
private static LayoutInflater inflater=null;
private SecondItem second_item;
private FirstItem first_item;
private int firstLength;
private boolean hasFirst = false;

public CustomAdapter(Activity a, JSONArray firstArray, JSONArray secondArray) {
    activity = a;
    first_item = new FirstItem (firstArray);
    second_item = new SecondItem (secondArray);
    firstLength = first_item.getLength();
    if (!firstArray.isNull(0)) {
        data=CustomUtils.concatJsonArray(firstArray, secondArray);
        hasFirst = true;
    }
    else
        data = secondArray;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    // TODO Auto-generated method stub
    return data.length();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public static class ViewHolder{
    public TextView txt_both;
    public TextView txt_first_only;
    ...
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        if (position < firstLength && hasFirst) {
            vi = inflater.inflate(R.layout.first_item, null);
        }
        else {
            vi = inflater.inflate(R.layout.second_item, null);
        }


        holder=new ViewHolder();
        holder.txt_both=(TextView)vi.findViewById(R.id.txt_both);
        ...

        if (position < firstLength && hasFirst) {
            holder.txt_firstOnly=(TextView)vi.findViewById(R.id.txt_firstOnly);
            ...

        }
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    if (position < firstLength && hasFirst) {
            holder.txt_both.setText(first_item.getContent(position));
            ...

这里是注释块,导致之前的空指针异常(错误点行holder.txt_firstOnly.setText):

        /*if (first_item.getStatus(position)==0) {
            holder.txt_firstOnly.setText(...);              

        }
        else if (first_item.getStatus(position)==1) {
            holder.txt_firstOnly.setText(...);
        }
        else if (first_item.getStatus(position)==2) {
            holder.txt_firstOnly.setText(...);

        } */

这是其余的代码:

        }
    else {
        holder.txt_both.setText(second_item.getContent(position - firstLength));
        ...

    }

    return vi;
}


}

【问题讨论】:

    标签: java android android-listview adapter


    【解决方案1】:

    您的自定义适配器应覆盖方法getViewTypeCount()getItemViewType (int position)。现在,操作系统认为所有视图都具有相同的视图类型,因此它任意传入先前创建的视图。如您所见,有时这不是该职位的正确视图。

    【讨论】:

    • 感谢您的回答.. 这真的很鼓舞人心.. 我对这些方法有很好的参考here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多