2

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 list appears fine. But on clicking the checkboxes and entering some text in the textboxes and scrolling down, and then up again, the data disappears.

In fact any change disappears, even if they were already checked. I uncheck them, then scroll down and up, the go back to checked. So basically, they go back whatever state they were when retrieved.

Any ideas why? Thanks.

Arun
  • 3,036
  • 3
  • 35
  • 57
  • 1
    I think android is recycling the views and does not save the state. Maybe there is some handler where you can save the changed values – Fender May 19 '11 at 11:12
  • 1
    Questions about ListView not keeping states of its row views are quite frequently asked here, i bet you will find a few easily. I found [this one](http://stackoverflow.com/questions/2937581/android-how-to-make-radiogroup-work-correctly-in-a-listview) particularly useful. – compostus May 19 '11 at 11:28

3 Answers3

4

You need to have an arraylist of the states of each item in the list,, then load these states each time the list item view is loaded.Do this by overriding GetView() method in the adapter and add your saved state to the list based on the item position

Hazem Farahat
  • 3,790
  • 2
  • 26
  • 42
2

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;
        }
DeRagan
  • 22,827
  • 6
  • 41
  • 50
0

Android does not render all ListView entries at once, but only those visible on the screen. When "new" List-Rows come into view the

public View getView(int position, View convertView, ViewGroup parent)

method of your Adapter gets called and the view is recreated. To fill in previousely saved values you probalby have to overwrite the getView method.

whlk
  • 15,487
  • 13
  • 66
  • 96