1

I have an expandable list view with checkboxes. When i click a child, an alertdialog and i choose the quantity and then the textView of a child changes. BUT when i scroll down the list and this child disappers from view , the list forget changed textview and set the old one. What's the reason?

6
  • put you code the expandable list and the code in your click listener
    – Fakher
    Nov 12, 2015 at 7:59
  • 1
    you need to store the quantity in the List so that whenever the view recreated it will refer from the List item. and also use setTag and getTag Nov 12, 2015 at 8:05
  • Do you use the ViewHolder pattern ? List keeps re-creates your List items when they are visible again, so you must save your items based on position and load them if they are already created instead of let the adapter create them again. Nov 12, 2015 at 8:10
  • @Gil no, i don't use ViewHolder. Should I ?
    – userpixel
    Nov 12, 2015 at 8:30
  • Yes. ViewHolder pattern should give you a re-use mechanism of your objects/list row items. It is not so difficult, if you would need a hand, please do notify me. Nov 12, 2015 at 8:41

1 Answer 1

0

ListView (and its descendent ExpandableListView) does NOT create and store the views forever; instead it creates them on-the-fly as needed.

Imagine a scenario where you have a ListView with a list containing 1000 items; but the views for only any 5 items can be visible on the screen at a given time. Do you think that ListView would create and maintain 1000 different views on the screen? That would be a waste of memory, and might cause the UI to lag.

Instead, ListView internally calls getView() function to obtain the view for each item and shows it on the screen. It does this every time the item is brought into the screen display, and only for those many number of items which can fit into the screen at a given time (ListView handles these things internally so you do not have to worry about this)

All you need to do is set the text in the corresponding list item, and use this text to populate the textview in getView(). Maintain a text String and create some form of getText() and setText(String) methods in whatever Object type you are using as an Item.

@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
    ......//initialize text view for this position
    Item item = getItem(position);
    textView.setText(item.getText());
    .......
    }

Once you set the text in the list item via alertDialog, Call notifyDataSetChanged() on the adapter to indicate that getView needs to be called again for all the views currently in display. In your Listener, pass a reference to the adapter of the ListView. When you set the quantity in the alertDialog, just use

    {
    ......
    adapter.getItem(position).setText(quantityText);
    adapter.notifyDataSetChanged();
    ......
    }

The first line sets the text in the item; the second line tells the adapter that the information in the items have changed and it needs to create the views again.

2
  • in expandable list instead of getItem should be getChild?
    – userpixel
    Nov 12, 2015 at 9:45
  • 1
    I am not sure, but I think getItem() is replaced by getGroup()/getChild() and getView() is replaced by getGroupView()/getChildView() respectively, depending on whether it's a list header or child
    – user5550987
    Nov 12, 2015 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.