android - ListView 布局返回空 View

标签 android android-layout android-activity android-listview android-adapter

我正在尝试制作具有多种布局的 ListView 。布局由数字 0-5 选择。所以我有 6 种可能的布局。我使用 switch case 来膨胀对应于数字的布局。我遇到了一些问题。首先,我得到了某些布局的副本,这些布局具有以前布局的值,甚至不应该存在。下面代码的另一个问题是,如果我滚动到按钮,最后一个元素的布局为案例 4,然后当我滚动到顶部并返回按钮时,它具有来自不同案例的布局。为什么会这样。此外,如果我尝试使用 settext() 设置 TextView ,我会收到一条错误消息,指出 TextView 为空。

@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 View ( View 的糟糕名称,顺便说一句):

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

其中一个可能是错误的。

关于android - ListView 布局返回空 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26105173/

相关文章:

java - 如何从 float 大数中删除 E?

android - Android "SuperNotCalledException:Activity did not call through to super.OnCreate()"错误

java - 按下“返回”按钮时返回到正确的 Activity ?

android - 找不到符号 Volley

android - ConstraintLayout 匹配兄弟 View 高度,以较高者为准

安卓工作室 : Cannot change layout button properties

java - 公共(public)静态变量和Android Activity 生命周期管理

Android Activity 识别无法正常工作

android - 使用菜单更改背景颜色

java - 如何以编程方式更改样式属性值?