How can I update a single row in a ListView?

前端 未结 11 2070
时光说笑
时光说笑 2020-11-22 17:07

I have a ListView which displays news items. They contain an image, a title and some text. The image is loaded in a separate thread (with a queue and all) and w

11条回答
  •  旧时难觅i
    2020-11-22 17:29

    The answers are clear and correct, I'll add an idea for CursorAdapter case here.

    If youre subclassing CursorAdapter (or ResourceCursorAdapter, or SimpleCursorAdapter), then you get to either implement ViewBinder or override bindView() and newView() methods, these don't receive current list item index in arguments. Therefore, when some data arrives and you want to update relevant visible list items, how do you know their indices?

    My workaround was to:

    • keep a list of all created list item views, add items to this list from newView()
    • when data arrives, iterate them and see which one needs updating--better than doing notifyDatasetChanged() and refreshing all of them

    Due to view recycling the number of view references I'll need to store and iterate will be roughly equal the number of list items visible on screen.

提交回复
热议问题