gpt4 book ai didi

java - Android NotifyDataSetChanged 和 BaseAdapter

转载 作者:行者123 更新时间:2023-12-02 07:21:42 25 4
gpt4 key购买 nike

我对这个适配器元素(NotifyDataSetChanged)有点困惑,因为当我使用它时,我的 ListView 没有更新。

我的工作场景:首先,我创建了一个 void,这样每当我更新 listview 项目上的某些内容时,我就可以再次调用我的 void。

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);

}

主要问题是,每当我更新数据库上的某些内容并调用“bindOrderListSorted”时,我的 ListView 就会更新,但正如预期的那样,它会再次重新绑定(bind), ListView 的位置再次变为零。

这是我之前的逻辑,但后来我发现,如果我有一长串项目怎么办?它不合适且用户友好,因为用户必须再次向下滚动才能找到他/她希望更新的项目

所以我了解了 NotifyDataSetChanged 并为此再次创建了一个 void

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

}

每当我更新某些内容时,都将其称为“NotifyDataChangedOrderListSorted”。

我的主要问题是我的 ListView 没有更新。我非常确定我的 orderList 具有更新的值,因为使用调试器和断点我期望的数据符合预期。这是为什么?

欢迎大家提出建议或意见。如果您想查看我的基本适配器,请这么说..

提前致谢

最佳答案

您正在创建一个新的 List 和 ArrayAdapter,因此 notifyDataSetChanged() 不起作用,并且 orderAdapter 不再引用 ListView 中的适配器。您有两个选择:

  1. NotifyDataChangedOrderListSorted() 方法中调用 setAdapter():

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

    但这与bindOrderListSorted()

  2. 相同
  3. 重用orderListorderAdapter:

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

    ...
    cursor.close();
    mySQLiteAdapter.close();
    orderAdapter.notifyDataSetChanged(); // Change this

    }

但是确实您应该使用 CursorAdapter,因为它们更快、更小......但这取决于您。

关于java - Android NotifyDataSetChanged 和 BaseAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com