BaseAdapter notifyDatasetChanged() called but getView() is never called

前端 未结 4 1139
梦毁少年i
梦毁少年i 2021-01-01 21:58

I have a custom adapter that visualize each row in the list of Orders.

public class OrderRowAdapter extends BaseAdapter implements OnClickListener {
    Ord         


        
相关标签:
4条回答
  • 2021-01-01 22:39

    It turns out the problem with my getView() not being called is because it is not visible. My layout xml has the upper TextView with fill_parent for its height. Thus the entire view only has that single TextView visible.

    Solution: check the graphical view of the layout in question to make sure the ListView is visible.

    0 讨论(0)
  • 2021-01-01 22:44

    If all above answers not working try with invalidateViews()

    ListView.invalidateViews() is used to tell the ListView to invalidate all its child item views (redraw them).

    Note that there not need to be an equal number of views than items. That's because a ListView recycles its item views and moves them around the screen in a smart way while you scroll.

     listView.invalidateViews()
    

    My sample implementation,

                        lvitems.post(new Runnable() {
                            @Override
                            public void run() {
                                lvitems.invalidateViews(); //invalidate old
                                CustomAdapter customAdapter=new CustomAdapter(context, names, images); // load new data
                                customAdapter.notifyDataSetChanged();// call notifydatasetChanged
                            }
                        });
    

    If any mistake in this answer please correct my mistakes.

    0 讨论(0)
  • 2021-01-01 22:46

    To change the content of your ListView, you must keep using the same reference to the List. Here you're creating another list and assigning it to the items_ variable (which does not contain the list itself, it's just a place to store a reference to a List), but your View still has a reference to the old list.

    Instead of items_ = new_order_list this should work :

    items_.clear();
    items_.addAll(new_order_list);
    

    EDIT :

    To explain it better, try to create a new variable named old_items :

    public void setNewOrderList(List<Order> new_order_list)
    {
        Log.d(TAG, "New Order List available. Num items = " + new_order_list.size());
    
        List<Order> old_items = items_; // store the reference to the old list
        items_ = new_order_list;
    
        Log.d(TAG, "items_.size() = " + items_.size());
        Log.d(TAG, "old_items.size() = " + old_items.size()); // The old list still exists, and it's the one used by your ListView
    
        notifyDataSetChanged();
    }
    
    0 讨论(0)
  • 2021-01-01 22:51

    Make sure BaseAdapter methods

    registerDataSetObserver(DataSetObserver observer) 
    unregisterDataSetObserver(DataSetObserver observer) 
    

    are not overridden.

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