Reputation: 837
How to update items in listview after update the row on database sqlite. I have put notifyDataSetChanged() but not success, there is not error, but not refresh data in row. The data will change if I click menu back and open list again. So I want update the data in row after I update data. Below structur my code, Thanks.
CustomBaseAdapter adapter;
ListView cListView;
in onCreate:
cListView = (ListView) findViewById(R.id.list_category);
List<ItemsArtikel> rowItems = (List<ItemsArtikel>) db.getAllCategory(katname);
adapter = new CustomBaseAdapter(this, rowItems);
cListView.setAdapter(adapter);
Query listcategory:
public List<ItemsArtikel> getAllCategory(String cat) {
SQLiteDatabase db = this.getWritableDatabase();
List<ItemsArtikel> cList = new ArrayList<ItemsArtikel>();
String selectQuery = "SELECT * FROM " + TABLE_CAT + " WHERE "
+ COLOM_KAT + "=\"" + cat + "\"";
Cursor c = db.rawQuery(selectQuery, null);
c.moveToFirst();
if (c.getCount() > 0) {
do {
ItemsArtikel kat = new ItemsArtikel();
kat.setID(c.getLong(c.getColumnIndex(COLOM_ID)));
kat.setName(c.getString(c.getColumnIndex(COLOM_NAME)));
kat.setLink(c.getString(c.getColumnIndex(COLOM_LINK)));
kat.setImage(c.getString(c.getColumnIndex(COLOM_IMAGE)));
kategoriList.add(kat);
} while (c.moveToNext());
}
return cList;
}
After i use for listview is presented for
to download images and update each row the database using AsyncTask when download image. Image success downloaded and row also success updated, Just not refresh in listview.
This is code onPostExecute when download image:
@Override
protected void onPostExecute(HashMap<String, Object> result) {
String id = (String) result.get("id");
String path = (String) result.get("image");
// update row in database
db.updateRowCategory(id, path);
cListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
Upvotes: 3
Views: 7499
Reputation: 674
Use following line before add adapter.
@Override
protected void onPostExecute(HashMap<String, Object> result) {
String id = (String) result.get("id");
String path = (String) result.get("image");
// update row in database
db.updateRowCategory(id, path);
cListView.Invalidate();
cListView.setAdapter(adapter);
cListView.Invalidate();
adapter.notifyDataSetChanged();
}
Upvotes: 0
Reputation: 1045
On calling notifyDataSetChanged() it will just notify the register view to update the content to reflect the changes.
In your case you are updating data in Database not in Adapter Data model. To reflect the changes in ListView
you need to updated the Adapter's
datamodel and then call notifyDataSetChanged();
OR
If your are rendering data directly from Database use CursorAdapter and implement onContentChanged ()
for updating ListView
Upvotes: 2