简体   繁体   中英

ListView ViewHolder item child View visibility changing

I'm having a ListView with a custom ArrayAdapter . The problem is that I cannot hide ( setVisibility(View.GONE) ) TextView in some elements of choice. Here is a minimized-readable summary part of my code:

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;
    }
}

What am I doing wrong here? How can I hide a view inside item in ListView if its content String is empty?

Add else part of if condition to change visibility from GONE to VISIBLE as in below code.

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;
    }
}


OR
try if (item.getDescription()==null || item.getDescription().trim().length() == 0) instead of if (TextUtils.isEmpty(item.getDescription()))

Check any white space should not be in your text. Try this-

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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