繁体   English   中英

listview布局返回空视图

[英]listview layout returns null views

我正在尝试制作具有多种布局的列表视图。 布局由数字0-5选择。 所以我有6种可能的布局。 我使用开关盒来增加与数字相对应的布局。 我有一些问题。 首先是获取某些布局的副本,这些布局具有以前布局的值,甚至不应该存在。 以下代码的另一个问题是,如果我滚动到按钮的最后一个元素的大小写为4,那么当我滚动到顶部并返回按钮的按钮时,它的布局是不同的大小写。 为什么会这样呢? 另外,如果我尝试使用settext()设置textview,则会收到一条错误消息,指出textview为null。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder1 = null;
    View v = convertView;

    q = getItemViewType(getType(type, position));

    if (v == null) {
        holder1 = new ViewHolder();

        switch (q) {
            case 0:
                v = LayoutInflater.from(context).inflate(R.layout.layout0, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage0);
                holder1.string = (TextView) v.findViewById(R.id.string0);
                holder1.string2 = (TextView) v.findViewById(R.id.string_two0);
                break;
            case 1:
                v = LayoutInflater.from(context).inflate(R.layout.layout1, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage1);
                holder1.string = (TextView) v.findViewById(R.id.string1);
                break;
            case 2:
                v = LayoutInflater.from(context).inflate(R.layout.layout2, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage2);
                holder1.string = (TextView) v.findViewById(R.id.string2);
                break;
            case 3:
                v = LayoutInflater.from(context).inflate(R.layout.layout3, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage3);
                holder1.string = (TextView) v.findViewById(R.id.string3);
                break;
            case 4:
                v = LayoutInflater.from(context).inflate(R.layout.layout4, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage4);
                holder1.string = (TextView) v.findViewById(R.id.string4);
                break;
            default:
                v = LayoutInflater.from(context).inflate(R.layout.layout5, parent, false);
                holder1.mainpic = (ImageView) v.findViewById(R.id.mainimage5);
                holder1.string = (TextView) v.findViewById(R.id.string5);
                break;
        }

        v.setTag(holder1);
    } else {
        holder1 = (ViewHolder) v.getTag();
    }

    ///holder1.string.settext(" some text")   error

    return v;
}











@Override
public int getItemViewType(int value) {

int type;
if(value ==0)
{
    type=0;
}

else if(value ==1)
{
    type=1;
}else if(value ==2)
{
    type=2;
}
else if(value ==3)
{
    type=3;
}
else if(value ==4)
{
    type=4;
}else
{
    type=5;
}



return type;

}

清理代码后,很明显,您有两次将string视图分配给string视图(视图的别名,顺便说一句)的流浪代码:

holder1.string = (TextView) v.findViewById(R.id.string0);
holder1.string = (TextView) v.findViewById(R.id.string_two0);

其中之一可能是错误的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM