24

I'm wondering if it is possible to rerender just one element in a listview? I assume by calling notifyDatasetChanged() is gonna rerender the whole list?

Thanks,

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
wei
  • 6,629
  • 7
  • 40
  • 52
  • indeed, notify... tells the list that the adapter as been modified. I'm not sure you can update one element by one. – Sephy Jun 01 '10 at 21:46
  • 1
    Here's a similar SO question: http://stackoverflow.com/questions/3724874/android-update-single-item-in-list – Pēteris Caune Dec 21 '10 at 19:28
  • 6
    Googlers, be aware **THESE QUESTIONS ARE VERY VERY OLD**. For modern Android (2014+) you simply use notifyDatasetChanged. it's incredibly efficient and only redraws what is on screen. It's essential to use the modern "HolderView" pattern these days. So it's all a non-issue. – Fattie Jun 26 '14 at 14:42

6 Answers6

19

you can't render (refresh) a single row, but instead you can get the requested view and make chages on it directly by calling yourListView.getChildAt(int VisiblePosition); where the visiblePostion is the position in the ListView minus yourListView.getFirstVisiblePosition()

Like this :

    View v = listViewItems.getChildAt(position - 
            listViewItems.getFirstVisiblePosition());
    v.setBackgroundColor(Color.GREEN);

I hope this helps...

Driss Bounouar
  • 3,182
  • 2
  • 32
  • 49
11

You can, but it's a bit convoluted. You would have to get the index of the first visible item in the list and then use that do decide how how far down in the list of visual items the item is that needs updated, then grab its view and update it there.

It's much easier to just call notifyDatasetChanged().

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • and how do you get the index of the first item visible in the list? There may be performance reasons why refreshing the whole list is not preferrable – Marchy Mar 31 '12 at 03:14
  • You just need to call `getFirstVisiblePosition()` on the ListView. http://developer.android.com/reference/android/widget/AdapterView.html#getFirstVisiblePosition() – CaseyB Apr 02 '12 at 16:56
  • @CaseyB can u help me on this issue http://stackoverflow.com/questions/18312539/update-the-value-of-item-in-the-listview-android?noredirect=1#comment26873838_18312539 – Developer Aug 19 '13 at 12:36
  • I am renaming filename within a listview but it doesn't refresh even with that call. Any idea? – Si8 Dec 03 '13 at 01:44
  • On what object do you call `notifyDatasetChanged()`? AFAIK, in Java, a method is identified by a name AND by its class. Without the class, this does not mean anything! – Vince Nov 10 '14 at 23:56
  • 1
    the question is how to refresh just one item. This solution make all the itmes refreshed. So this is not the right response. – Lisitso Mar 17 '15 at 18:31
4

Also you can use this:

myListView.invalidateViews();
Bob
  • 22,810
  • 38
  • 143
  • 225
3

This is how I did it:

Your items (rows) must have unique ids so you can update them later. Set the tag of every view when the list is getting the view from adapter. (You can also use key tag if the default tag is used somewhere else)

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View view = super.getView(position, convertView, parent);
    view.setTag(getItemId(position));
    return view;
}

For the update check every element of list, if a view with given id is there it's visible and update must be performed on it.

private void update(long id)
{

    int c = list.getChildCount();
    for (int i = 0; i < c; i++)
    {
        View view = list.getChildAt(i);
        if ((Long)view.getTag() == id)
        {
            // update view
        }
    }
}

It's actually easier than other methods and better when you dealing with ids not positions! Also you must consider scenario when your view get invisible and visible again.

Ali
  • 21,572
  • 15
  • 83
  • 95
  • The tag idea helped me in my case - I had dynamic numbers of Views inflated from the same layout file and had to somehow check which one I was to change. – JakeP Jul 23 '14 at 11:02
3
dataAdapter.remove(dataAdapter.getItem(clickedpos));

dataAdapter.insert(t.getText().toString(), clickedpos);
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
greeshma
  • 57
  • 1
1

You need to keep track of your adapter (or custom adapter if you are set on fancy features). When you change the data for an item, simply change the fields you are interested in , in your adapter. Then call notifyDatasetChanged , and the changes will be reflected in your listview.

Note that this approach works exactly the same for Gallery Views as well.

radhoo
  • 2,877
  • 26
  • 27