okay, so after going through tons of sites and blogs I have finally decided to ask this 'not so FAQ'.
I have 2 listViews. One as the 'category LV' and other as the 'item LV'. The functionality is quite simple- When the user clicks a category cell in the 'category LV', all the items matching that category should filter and appear in the 'item LV'. I do this by assigning the 'category integer' to every item in the database. So, when a category is tapped its integer id is taken and all the ids matching with it in the item database are loaded in the cursor.
PROBLEM: I use the adapter.changeCursor(newCursor) method to change the underlying data in the adapter. According to the docs, this method replaces and closes the previously loaded cursor. I am getting this error
11-08 11:54:15.939: E/AndroidRuntime(2056): android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
Down below is the code explained:
itemAdapter=new CategoryCursorAdapter(this, itemCursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER, "item_name",1,date,db);
categoryLV.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String s=((TextView)arg1.findViewById(R.id.category_name)).getText().toString();
if(s.equalsIgnoreCase("all")){
itemAdapter.changeCursor(null);
itemAdapter.notifyDataSetChanged();
}
else{
itemAdapter.changeCursor(db.getItemCursor(s));
itemAdapter.notifyDataSetChanged();
}
}
});
I get the error in the CursorAdapter class:
public void bindView(View view, Context context, Cursor cursor) {
if(mcolumn==CATEGORY_LV){
TextView tv=(TextView)view.findViewById(R.id.category_name);
tv.setText(mCursor.getString(mCursor.getColumnIndex(mColumnName)));
tv.setTextSize(20);
}
if(mcolumn==ITEM_LV){
TextView item=(TextView)view.findViewById(R.id.item_name);
TextView stock=(TextView)view.findViewById(R.id.stock_status);
String s=mCursor.getString(mCursor.getColumnIndex(mColumnName));
Button drag=(Button)view.findViewById(R.id.drag_button);
LinearLayout lv=(LinearLayout)view.findViewById(R.id.layout);
drag.setFocusable(false);
item.setText(s);
item.setTextSize(20);
stock.setText("Stock:"+mdb.getStockStatus(mdb.getItemCode(s), mDate));
stock.setTextSize(15);
}
}
One thing to note here is that I am using the same Adapter class to power both the listViews. However i dont think this can be a problem.