3

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();
}

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hello Animesh, thanks for reply. I have update my post so how about to use onContentChanged(), if I can use a query like the one above.
0

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();
}

1 Comment

Before where? can you please copy my code to your answer, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.