比较字符串时选择了错误的ListView项

智者

适配器:

public class CategoryAdapter extends ArrayAdapter<Category> {
    public CategoryAdapter(Context context, ArrayList<Category> categories){
        super(context, 0, categories);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Category category = getItem(position);

        if(convertView==null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_game_choose_category, parent, false);
        }

        TextView categoryName = (TextView) convertView.findViewById(R.id.category);
        TextView categorySubtext = (TextView) convertView.findViewById(R.id.categorySub);
        TextView categoryQuestionsCount = (TextView) convertView.findViewById(R.id.categoryQuestionsCount);

        categoryName.setText(category.categoryName); // Populate
        categorySubtext.setText(category.categorySubtext);
        categoryQuestionsCount.setText(category.categoryQuestionsCount);


        return convertView;
    }
}

模型:

public class Category {
   public Category(String categoryName,
                    String categoryId,
                    String categoryParentId,
                    String categoryQuestionsCount,
                    String categorySubtext) {
        this.categoryName = categoryName;
        this.categoryId = categoryId;
        this.categoryParentId = categoryParentId;
        this.categoryQuestionsCount = categoryQuestionsCount;
        this.categorySubtext = categorySubtext;
    }
}

我想隐藏为空的TextView,因此getView在Adapter方法中我写了类似以下内容:

if(category.categorySubtext.equals("")) { categorySubtext.setVisibility(View.GONE); }

但是突然之间,即使是HAD中“ categorySubtext”填充的项目也被此子句调用!在我的ListView中滚动后,所有项目都具有可见性:已消失。

我也尝试致电category.categorySubtext.isEmpty()category.categorySubtext.trim().equalsIgnoreCase()-每次我得到相同的行为时。

所有建议表示赞赏!

Mattias Isegran Bergander

该视图可重用于ListView的不同行/项目,并且您永远都不能将它们设置为可见。

使用类似这样的东西:

categorySubtext.setVisibility(
         category.categorySubtext.isEmpty() ? View.GONE : View.VISIBLE
     );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章