ListView ViewHolder项目子视图的可见性更改

时间:2019-05-21 11:02:06

标签: java android listview android-listview android-arrayadapter

我有一个ListView和一个自定义ArrayAdapter。问题是我无法在某些选择的元素中隐藏(setVisibility(View.GONE)TextView。这是我的代码的最小可读的摘要部分:

public class ListViewAdapter extends ArrayAdapter<ItemModel> {

    private int listItemLayout;

    static class ViewHolder {
        private TextView description;
    }

    public ListViewAdapter(Context context, int layoutId, List<ItemModel> itemList) {
        super(context, layoutId, itemList);
        this.listItemLayout = layoutId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemModel item = getItem(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.description = convertView.findViewById(R.id.text_desription);
        viewHolder.description.setText(item.getDescription());
        if (TextUtils.isEmpty(item.getDescription())) {
            viewHolder.description.setVisibility(View.GONE);
        }
        return convertView;
    }
}

我在这里做错了什么?如果内容ListView为空,如何在String中隐藏项目内部的视图?

2 个答案:

答案 0 :(得分:0)

添加if条件的其他部分,以将可见性从GONE更改为VISIBLE,如下面的代码所示。

public class ListViewAdapter extends ArrayAdapter {

    private int listItemLayout;

    static class ViewHolder {
        private TextView description;
    }

    public ListViewAdapter(Context context, int layoutId, List itemList) {
        super(context, layoutId, itemList);
        this.listItemLayout = layoutId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemModel item = getItem(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.description = convertView.findViewById(R.id.text_desription);
        viewHolder.description.setText(item.getDescription());
        if (TextUtils.isEmpty(item.getDescription())) {
            viewHolder.description.setVisibility(View.GONE);
        }else{
            viewHolder.description.setVisibility(View.VISIBLE);
        }
        return convertView;
    }
}



尝试if (item.getDescription()==null || item.getDescription().trim().length() == 0) 而不是if (TextUtils.isEmpty(item.getDescription()))

答案 1 :(得分:0)

检查文本中是否不应包含空格。试试这个

       if (TextUtils.isEmpty(item.getDescription().trim()) ||item.getDescription().trim().length()==0 ) {
            viewHolder.description.setVisibility(View.GONE);
        }

    else{
       viewHolder.description.setVisibility(View.VISIBLE);
      }