Reputation: 491
Im a little bit confused about this adapter element (NotifyDataSetChanged) cause when I use it, my listview doesnt update.
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.
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.
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
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.
My main problem is my listview isnt updating. I am quite sure that my orderList has the updated value cause using debugger and breakpoints the data I am expecting is as expected. 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
Upvotes: 0
Views: 1467
Reputation: 86948
You are creating a new List and ArrayAdapter so notifyDataSetChanged()
has no effect and orderAdapter
no longer references the adapter in your ListView. You have two choices:
Call setAdapter()
in your NotifyDataChangedOrderListSorted()
method:
...
orderAdapter =new OrderListAdapter(this, orderList,context, sort);
listViewSearchPanel.setAdapter(orderAdapter);
But this identical to bindOrderListSorted()
Reuse orderList
and orderAdapter
:
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.
Upvotes: 2