Refresh viewlist after sqlite database refresh

How to update items in a list after updating a row in sqlite database. I put notifyDataSetChanged () but no success, no error, but not updating data in row. The data is changed if I click the menu button again and open the list again. So I want to update the data in the row after updating the data. Structure my code below, 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);

      

List of categories:

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 use, I am presented for listview for

to load images and update each row of the database using AsyncTask on image load. The success of the image has been uploaded and also a number of rows have been successfully updated, just not updating in the list.

This is the onPostExecute code when loading the 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();
}

      

+3


source to share


2 answers


When notifyDataSetChanged () is called, it simply notifies the register to update the content to reflect the changes.

In your case, you are updating data in the Database not in the adapter data model. To reflect the changes in ListView

, you need to update the datamodel Adapter's

and then call notifyDataSetChanged ();



OR

If you are passing data directly from the database use CursorAdapter and implement onContentChanged ()

to update the ListView

+2


source


Use the following line before adding the 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();
}

      

0


source







All Articles