开发者

Android: Is it possible to refresh just one item in a listview? [duplicate]

This question already has answers here: Android ListView Refresh Single Row 开发者_如何学Go (7 answers) Closed 8 years ago.

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,


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...


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().


Also you can use this:

myListView.invalidateViews();


dataAdapter.remove(dataAdapter.getItem(clickedpos));

dataAdapter.insert(t.getText().toString(), clickedpos);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜