0

i need little solution for refreshing listview after delete data from database. i realized that i can delete data from database with this function of activity :

helper.delete(String.valueOf(editID));
adapter.notifyDataSetChanged();
break;

and in my DBHelper :

public void delete(String id) {
    String[] args = {id};
    getWritableDatabase().delete(TABLE_NAME, "_ID=?", args);
    getWritableDatabase().close();

This code works for deleting database and item, with adapter.notifyDataSetChanged(); But the listView just refresh when i closed application then opened it again. I need a way how to refresh listView instantly. some people used adapter.remove(position);, to refresh instantly, then call .notifyDataSetChanged();. But it not worked with my custom adapter.

I used this kind of adapter :

class ListAdapter extends CursorAdapter{
    @SuppressWarnings("deprecation")
    ListAdapter(Cursor c){
        super(MainActivity.this, c);
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        // TODO Auto-generated method stub
        ListHolder holder = (ListHolder)row.getTag();
        holder.populateFrom(c, helper);         
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.custom_listview, parent, false);
        ListHolder holder = new ListHolder(row);
        row.setTag(holder);
        return (row);
    }       
}

static class ListHolder {
    private TextView nama = null, kategori=null;
    private ImageView icon=null;
    private View row = null;

    ListHolder(View row){
        this.row=row;

        nama=(TextView)row.findViewById(R.id.main_title_name);
        kategori=(TextView)row.findViewById(R.id.sub_title_name);
        icon=(ImageView)row.findViewById(R.id.thumbnail);
    }

    void populateFrom(Cursor c, DBHelper helper){
        nama.setText(helper.getNamaLok(c));
        kategori.setText(helper.getKatgLok(c));

        byte[] byteArray = helper.getImglok(c);
        Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0 ,byteArray.length);
        icon.setImageBitmap(bm);

    }               
}

Sorry for my bad english, and.. Hope there is a way to solve this.. Thanks in advance.

zuzu
  • 21
  • 1

1 Answers1

0

You need to use .notifyDataSetChanged(); in correct way. Remember that method .notifyDataSetChanged() says with adapter that the data has been changed and you need to refresh.

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

So all you need to do is change the data set and then notify to the adapter. With CussorAdapter, data set is the cursor, So you should change you cursor be below ways:

  1. requery() to update your cursor
  2. Use changeCursor(Cursor cursor) to change to new cursor.

Maybe this Link will be helpful for you.

Community
  • 1
  • 1
ThaiPD
  • 3,503
  • 3
  • 30
  • 48
  • For my CursorAdapter case, where i must initialized requery()? in bindView or newView, or in button function delete? – zuzu Jul 06 '15 at 05:45