ListView not updating properly CursorAdapter after swapCursor

Viewed 3142

I've got a CursorAdapter implementation that is incredibly basic. I just overrode newView and bindView (Code below).

What I'm doing is I add a row to my table, create a new cursor that queries the table (with the new row), change the cursor in the adapter and then notify the data change.

What's happening is a new row is added to the ListView (as is expected), but the data mirrors the first item in the list. Fun part: when I change orientation or simply reload the fragment (via tab change), the list data is proper.

I've created a crude work-around, and that is to simply change the cursor and call list.setAdapter again. This makes it update just fine.

Is this a decent plan, or is there a way to fix this without the work-around?

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());
    // set the list item info from the cursor
    // set the encounter title
    TextView t = (TextView)view.findViewById(R.id.encounter_creature_list_name);
    t.setText(String.format(t.getText().toString(), rowID + ""));

    // set the creature count
    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've debugged the getView method and the data that is returning from my Cursor is accurate and the TextViews are getting the right data, it's just displaying a mirror of the first item.

Cursor change(not refreshing properly):

private void refreshList(){
    Cursor c = GameTable.query(GameSQLDataSource.getDatabase());

    EncounterAdapter adapter = (EncounterAdapter) mList.getAdapter();
    adapter.changeCursor(c);
    adapter.notifyDataSetChanged();
}

Cursor change(work around):

private void refreshList(){
    Cursor c = GameTable.query(GameSQLDataSource.getDatabase());

    EncounterAdapter adapter = (EncounterAdapter) mList.getAdapter();
    adapter.changeCursor(c);
    mList.setAdapter(adapter);
}
0 Answers
Related