简体   繁体   English

Android NotifyDataSetChanged和BaseAdapter

[英]Android NotifyDataSetChanged And BaseAdapter

Im a little bit confused about this adapter element (NotifyDataSetChanged) cause when I use it, my listview doesnt update. 我对这个适配器元素(NotifyDataSetChanged)有点困惑,因为当我使用它时,我的listview没有更新。

My work scenario: First ive created a void so that whenever I Update something on listview's item I could just call again my void. 我的工作场景:首先我创建了一个空白,这样每当我在listview的项目上更新某些内容时,我就可以再次调用我的空白。

private void bindOrderListSorted(String sort){
    orderList = new ArrayList<Order>();
    mySQLiteAdapter = new SQLiteAdapter(context);
    mySQLiteAdapter.openToRead();
    String selectQuery = "MY QUERY...";
    Cursor cursor =mySQLiteAdapter.read(selectQuery); 
    while(cursor.moveToNext())
    {
        Order order = new Order();
        order.orderdDesc = cursor.getString(0);
        order.orderitemCode = cursor.getString(1);
        order.orderitemID = cursor.getString(2);
        order.orderPrice = cursor.getString(3);
        order.qtyOrdered = cursor.getString(4);
        order.stocksQty = cursor.getString(5);
        orderList.add(order);
    }
    cursor.close();
    mySQLiteAdapter.close();
    orderAdapter =new OrderListAdapter(this, orderList,context, sort);      
    listViewSearchPanel.setAdapter(orderAdapter);

}

The main problem about this is that whenever I update something on my database and call that "bindOrderListSorted" my listview updates but as expected it will rebind again that the position of the listview goes to zero again. 这个问题的主要问题是每当我更新我的数据库并调用“bindOrderListSorted”时,我的listview会更新,但正如预期的那样,它会再次重新绑定listview的位置再次变为零。

Thats my logic before but then as I found out, what if I have a long list of items? 这是我以前的逻辑,但后来我发现,如果我有一长串的项目怎么办? It will not be suitable and user friendly because the user must scroll down again to find the item(s) he/she wishes to update 它不合适且用户友好,因为用户必须再次向下滚动以找到他/她希望更新的项目

so ive learned about NotifyDataSetChanged and created a void again for that 所以我已经了解了NotifyDataSetChanged并为此再次创建了一个空白

private void NotifyDataChangedOrderListSorted(String sort){
    orderList = new ArrayList<Order>();
    mySQLiteAdapter = new SQLiteAdapter(context);
    mySQLiteAdapter.openToRead();
    String selectQuery = "MY QUERY... SAME AS BEFORE";
    Cursor cursor =mySQLiteAdapter.read(selectQuery); 
    while(cursor.moveToNext())
    {
        Order order = new Order();
        order.orderdDesc = cursor.getString(0);
        order.orderitemCode = cursor.getString(1);
        order.orderitemID = cursor.getString(2);
        order.orderPrice = cursor.getString(3);
        order.qtyOrdered = cursor.getString(4);
        order.stocksQty = cursor.getString(5);
        orderList.add(order);
    }

    cursor.close();
    mySQLiteAdapter.close();
    orderAdapter =new OrderListAdapter(this, orderList,context, sort);  
    orderAdapter.notifyDataSetChanged();

}

And call that "NotifyDataChangedOrderListSorted" whenever I updated something. 每当我更新某些内容时,都会调用“NotifyDataChangedOrderListSorted”。

My main problem is my listview isnt updating. 我的主要问题是我的listview没有更新。 I am quite sure that my orderList has the updated value cause using debugger and breakpoints the data I am expecting is as expected. 我很确定我的orderList有更新的值因为使用调试器和断点我期待的数据是预期的。 Why is that? 这是为什么?

Please feel free to recommend suggestion or comments. 请随时推荐建议或意见。 If you want to see my base adapter please say it so.. 如果你想看到我的基础适配器请说出来..

THANKS IN ADVANCE 提前致谢

You are creating a new List and ArrayAdapter so notifyDataSetChanged() has no effect and orderAdapter no longer references the adapter in your ListView. 您正在创建一个新的List和ArrayAdapter,因此notifyDataSetChanged()无效, orderAdapter不再引用ListView中的适配器。 You have two choices: 你有两个选择:

  1. Call setAdapter() in your NotifyDataChangedOrderListSorted() method: NotifyDataChangedOrderListSorted()方法中调用setAdapter()

     ... orderAdapter =new OrderListAdapter(this, orderList,context, sort); listViewSearchPanel.setAdapter(orderAdapter); 

    But this identical to bindOrderListSorted() 但这与bindOrderListSorted()完全相同

  2. Reuse orderList and orderAdapter : 重用orderListorderAdapter

     private void NotifyDataChangedOrderListSorted(String sort){ orderList.clear(); // Change this mySQLiteAdapter = new SQLiteAdapter(context); mySQLiteAdapter.openToRead(); ... cursor.close(); mySQLiteAdapter.close(); orderAdapter.notifyDataSetChanged(); // Change this } 

But really you should use a CursorAdapter since they are faster and smaller... but it's up to you. 实际上你应该使用CursorAdapter,因为它们更快更小......但这取决于你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM