I'm trying to refresh my ListView whenever I delete an item on it. But setting a notifyDataSetChanged();
is giving me this error:
Cannot refer to a non-final variable adapter inside an inner class defined in a different method.
The method notifyDataSetChanged() is undefined for the type ListAdapter.
ArrayList <HashMap <String, String> > data = dataHolder.getAllData();
dataHolder.getAllData();
if(data.size() !=0){
ListView lv = (ListView) findViewById(R.id.datalist);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.dataentry, new String[]{"unique_id","lastName"}, new int[]{R.id.unique_id,R.id.last_name});
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setTitle("Delete?");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setMessage("Delete selected item?");
adb.setCancelable(false);
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Delete", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
TextView uID = (TextView) findViewById(R.id.unique_id);
String unique_id = uID.getText().toString();
dataHolder.deleteData(unique_id);
adapter.notifyDataSetChanged();
}
});
adb.show();
return false;
}
});
}
Then it suggests to change it to:
((BaseAdapter) adapter).notifyDataSetChanged();
final ListAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.dataentry, new String[]{"unique_id","lastName"}, new int[]{R.id.unique_id,R.id.last_name});
But it's not refreshing the list after changing to that. What is the solution for this?