I have a CursorAdapter implementation that is incredibly simple. I just overridden newView and bindView (code below).
What I am doing is adding a row to my table, creating a new cursor that queries the table (with a new row), changing the cursor in the adapter and then notifying me of data changes.
What happens on a new line is added to the ListView (as expected), but the data reflects the first item in the list. The wonderful part: when I change the orientation or just reload the fragment (by changing the tab), the list data is correct.
I created a tough job, and that is just changing the cursor and calling list.setAdapter again. This makes its update just fine.
Is this a decent plan, or is there a way to fix this without a workaround?
CODE:
CursorAdapter:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.list_item_encounters, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int rowID = cursor.getInt(GameTable.COLUMN_ID.getNum());
TextView t = (TextView)view.findViewById(R.id.encounter_creature_list_name);
t.setText(String.format(t.getText().toString(), rowID + ""));
int count = EncounterTable.getCreatureCount(GameSQLDataSource.getDatabase(), rowID);
t = (TextView)view.findViewById(R.id.encounter_creature_list_hp);
t.setText(String.format(t.getText().toString(), count +""));
}
I debugged the getView method, and the data returned from my cursor is accurate, and the TextViews get the correct data, it just displays the mirror of the first element.
Cursor change (not refreshing correctly):
private void refreshList(){
Cursor c = GameTable.query(GameSQLDataSource.getDatabase());
EncounterAdapter adapter = (EncounterAdapter) mList.getAdapter();
adapter.changeCursor(c);
adapter.notifyDataSetChanged();
}
Change of cursor (work around):
private void refreshList(){
Cursor c = GameTable.query(GameSQLDataSource.getDatabase());
EncounterAdapter adapter = (EncounterAdapter) mList.getAdapter();
adapter.changeCursor(c);
mList.setAdapter(adapter);
}