How can I update a single row in a ListView?

前端 未结 11 1975
时光说笑
时光说笑 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条回答
  • 2020-11-22 17:23

    I found the answer, thanks to your information Michelle. You can indeed get the right view using View#getChildAt(int index). The catch is that it starts counting from the first visible item. In fact, you can only get the visible items. You solve this with ListView#getFirstVisiblePosition().

    Example:

    private void updateView(int index){
        View v = yourListView.getChildAt(index - 
            yourListView.getFirstVisiblePosition());
    
        if(v == null)
           return;
    
        TextView someText = (TextView) v.findViewById(R.id.sometextview);
        someText.setText("Hi! I updated you manually!");
    }
    
    0 讨论(0)
  • 2020-11-22 17:23

    This question has been asked at the Google I/O 2010, you can watch it here:

    The world of ListView, time 52:30

    Basically what Romain Guy explains is to call getChildAt(int) on the ListView to get the view and (I think) call getFirstVisiblePosition() to find out the correlation between position and index.

    Romain also points to the project called Shelves as an example, I think he might mean the method ShelvesActivity.updateBookCovers(), but I can't find the call of getFirstVisiblePosition().

    AWESOME UPDATES COMING:

    The RecyclerView will fix this in the near future. As pointed out on http://www.grokkingandroid.com/first-glance-androids-recyclerview/, you will be able to call methods to exactly specify the change, such as:

    void notifyItemInserted(int position)
    void notifyItemRemoved(int position)
    void notifyItemChanged(int position)
    

    Also, everyone will want to use the new views based on RecyclerView because they will be rewarded with nicely-looking animations! The future looks awesome! :-)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 17:30

    get the model class first as global like this model class object

    SampleModel golbalmodel=new SchedulerModel();
    

    and initialise it to global

    get the current row of the view by the model by initialising the it to global model

    SampleModel data = (SchedulerModel) sampleList.get(position);
                    golbalmodel=data;
    

    set the changed value to global model object method to be set and add the notifyDataSetChanged its works for me

    golbalmodel.setStartandenddate(changedate);
    
    notifyDataSetChanged();
    

    Here is a related question on this with good answers.

    0 讨论(0)
  • 2020-11-22 17:31

    I made up another solution, like RecyclyerView method void notifyItemChanged(int position), create CustomBaseAdapter class just like this:

    public abstract class CustomBaseAdapter implements ListAdapter, SpinnerAdapter {
        private final CustomDataSetObservable mDataSetObservable = new CustomDataSetObservable();
    
        public boolean hasStableIds() {
            return false;
        }
    
        public void registerDataSetObserver(DataSetObserver observer) {
            mDataSetObservable.registerObserver(observer);
        }
    
        public void unregisterDataSetObserver(DataSetObserver observer) {
            mDataSetObservable.unregisterObserver(observer);
        }
    
        public void notifyDataSetChanged() {
            mDataSetObservable.notifyChanged();
        }
    
        public void notifyItemChanged(int position) {
            mDataSetObservable.notifyItemChanged(position);
        }
    
        public void notifyDataSetInvalidated() {
            mDataSetObservable.notifyInvalidated();
        }
    
        public boolean areAllItemsEnabled() {
            return true;
        }
    
        public boolean isEnabled(int position) {
            return true;
        }
    
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            return getView(position, convertView, parent);
        }
    
        public int getItemViewType(int position) {
            return 0;
        }
    
        public int getViewTypeCount() {
            return 1;
        }
    
        public boolean isEmpty() {
            return getCount() == 0;
        } {
    
        }
    }
    

    Don't forget to create a CustomDataSetObservable class too for mDataSetObservable variable in CustomAdapterClass, like this:

    public class CustomDataSetObservable extends Observable<DataSetObserver> {
    
        public void notifyChanged() {
            synchronized(mObservers) {
                // since onChanged() is implemented by the app, it could do anything, including
                // removing itself from {@link mObservers} - and that could cause problems if
                // an iterator is used on the ArrayList {@link mObservers}.
                // to avoid such problems, just march thru the list in the reverse order.
                for (int i = mObservers.size() - 1; i >= 0; i--) {
                    mObservers.get(i).onChanged();
                }
            }
        }
    
        public void notifyInvalidated() {
            synchronized (mObservers) {
                for (int i = mObservers.size() - 1; i >= 0; i--) {
                    mObservers.get(i).onInvalidated();
                }
            }
        }
    
        public void notifyItemChanged(int position) {
            synchronized(mObservers) {
                // since onChanged() is implemented by the app, it could do anything, including
                // removing itself from {@link mObservers} - and that could cause problems if
                // an iterator is used on the ArrayList {@link mObservers}.
                // to avoid such problems, just march thru the list in the reverse order.
                mObservers.get(position).onChanged();
            }
        }
    }
    

    on class CustomBaseAdapter there is a method notifyItemChanged(int position), and you can call that method when you want update a row wherever you want (from button click or anywhere you want call that method). And voila!, your single row will update instantly..

    0 讨论(0)
  • 2020-11-22 17:38

    I used the code that provided Erik, works great, but i have a complex custom adapter for my listview and i was confronted with twice implementation of the code that updates the UI. I've tried to get the new view from my adapters getView method(the arraylist that holds the listview data has allready been updated/changed):

    View cell = lvOptim.getChildAt(index - lvOptim.getFirstVisiblePosition());
    if(cell!=null){
        cell = adapter.getView(index, cell, lvOptim); //public View getView(final int position, View convertView, ViewGroup parent)
        cell.startAnimation(animationLeftIn());
    }
    

    It's working well, but i dont know if this is a good practice. So i don't need to implement the code that updates the list item two times.

    0 讨论(0)
提交回复
热议问题