Ando Masahashi
Ando Masahashi

Reputation: 3122

Update or change adapter in Listview

I have question for list-view please see my below code

private void set_adapter() {

    Cursor list_scan_curson = scan_result.getScanResult();
    List<String> list_scan = new ArrayList<String>();
    try {

        if (list_scan_curson.moveToFirst()) {
            do {

                String name = list_scan_curson.getString(0);

                // Adding contact to list
                list_scan.add(name);
            } while (list_scan_curson.moveToNext());
        }

        ArrayAdapter<String> list_item_scan = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, list_scan);
        this.list_scan.setAdapter(list_item_scan);

    } catch (Exception e) {

    }

}

now i want to change or update adapter when my database update and based on that list-view will update so can any buddy tell me how to do this

Upvotes: 1

Views: 6569

Answers (3)

Mohan
Mohan

Reputation: 311

If you want to update data from list use adapter.notifyDataSetChanged(). If you want to update data from database you have to trigger some actions so use onScrollchanged listener or use some background thread.

Upvotes: 1

Philip Sheard
Philip Sheard

Reputation: 5825

You create a new cursor, and replace the old one, something like this:

Private ArrayAdapter<String> mAdapter; // adapter declared as field
...
mAdapter =  ..., // create adapter
...
mAdapter.notifyDataSetChanged(); // update listview

Upvotes: 2

Pradip
Pradip

Reputation: 3177

You can implement some logic like below[checkIstheDataVersionChanged(Object CurrentVersion)] to check what type of change/update will affect your dataset and reload the listView using set_adapter.

boolean isDataSetChanged = checkIstheDataVersionChanged(Object CurrentVersion);

if(isDataSetChanged )
{
 set_adapter();
}

Upvotes: 0

Related Questions