Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update listview after update database sqlite

Tags:

android

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();
}
like image 684
Dave Jackson Avatar asked Feb 21 '26 07:02

Dave Jackson


1 Answers

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

like image 111
Animesh Sinha Avatar answered Feb 23 '26 21:02

Animesh Sinha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!