How to hide an item in a listview in Android

后端 未结 11 1055
天涯浪人
天涯浪人 2020-12-03 07:15

I know, this question was asked before, but I haven\'t seen a working answer for it.

Is there any way to hide some items in a ListView without changing

11条回答
  •  天命终不由人
    2020-12-03 07:48

    Here my solution:

    public class MyAdapter extends ArrayAdapter {
    
        public MyAdapter(Context context, int resId) {
            super(context, resId);
        }
    
        private List visibleEntries() {
    
            List result = new ArrayList();
    
            int i = 0;
            try {
                while (true) {
                    if (getItem(i).isVisible())
                        result.add(getItem(i));
                    i++;
                }
    
            } catch(Exception e) {
    
            }
    
            return result;
    
        }
    
        @Override
        public int getCount() {
            return visibleEntries().size();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            LinearLayout layout = 
                convertView == null ? 
                    (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.entry, parent, false) :
                    (LinearLayout) convertView;
    
            MyEntry entry = visibleEntries().get(position);
    
            ...
    
            return layout;
        }
    }
    

提交回复
热议问题