2013-01-03 34 views
0

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

我的工作场景: 首先创建了一个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”我的列表视图更新,但预期它会再次重新绑定的列表视图的位置再次进入零。

这就是我的逻辑之前,但随后我发现,如果我有一长串项目?这不会是合适的和用户友好的,因为用户必须再次向下滚动,找到项目(S),他/她希望更新

所以香港专业教育学院了解到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(); 

} 

并且每当我更新一些东西时调用“NotifyDataChangedOrderListSorted”。

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

请随时推荐建议或意见。如果你想看到我的基本适配器请把它说出来..

在此先感谢

回答

2

您正在创建一个新的列表和ArrayAdapter所以notifyDataSetChanged()没有任何作用,orderAdapter不再引用了您的ListView适配器。你有两个选择:

  1. 呼叫setAdapter()NotifyDataChangedOrderListSorted()方法:

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

    但是,这等同于bindOrderListSorted()

  2. 重用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,因为它们更快更小......但这取决于您。

+0

我使用编号2 @Sam – Androyds

+0

感谢您的回答。它正在工作。我读了光标适配器以供将来参考..感谢您解决我的问题 – Androyds

+0

很高兴我能帮助,祝你好运! – Sam

相关问题