Values disappearing from a Listview on Scroll - Android

前端 未结 3 2020
难免孤独
难免孤独 2020-12-20 04:18

I have a custom adapter extended from the SimpleCursorAdapter. Using this I\'m binding a ListView which contains a checkbox and Two textboxes. On opening the activity, the l

3条回答
  •  不思量自难忘°
    2020-12-20 04:48

    Listview tends to recreate its views every time your list is scrolled up or down. You need to have some kind of model class that can save the state of your checkbox and textbox in memory in case some change is done(for that particular row) and later display it on the view.

    As mentioned on other answers in this post u can use getview to programatically induce values that you have stored in your model classes to your views based on the list view position.

    Something like this

     @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                System.out.println("getView " + position + " " + convertView);
                ViewHolder holder = null;
                if (convertView == null) 
                {
                    convertView = mInflater.inflate(R.layout.item1, null);
                    holder = new ViewHolder();
                    holder.textView = (TextView)convertView.findViewById(R.id.text);
                    convertView.setTag(holder);
                } 
               else {
                    holder = (ViewHolder)convertView.getTag();
                }
               // Pass on the value to your text view like this. You can do it similarly for a check box as well
                holder.textView.setText(mData.get(position));
                return convertView;
            }
    

提交回复
热议问题