0

How can I reload a ListView from inside an AlertDialog? This AlertDialog is spawned by a ContectMenu.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    registerForContextMenu(getListView());
    ......
    MatrixCursor cursor;
    cursor = NameManager.getnameList();
    ........
    final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.row, cursor, from, to);
    setListAdapter(adapter);
}
.......
.......
case R.id.delete:
            new AlertDialog.Builder(this)
            .setTitle("Delete " + cursor.getString(1))
            .setMessage("Are you sure?")
            .setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                @Override
                public void onClick(
                        DialogInterface dialogInterface, int i) {
                    try {
                        .......
                        // TODO reload listview

                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                }
            }).setNeutralButton("Cancel", null)
            .create().show();
            return true;
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134

2 Answers2

0

You can call the listview's adapter's notifyDataSetChanged() or notifyDataSetInvalidated()

Daniel Chow
  • 201
  • 1
  • 5
0

Since I have like 8 other AlertDialogs that will need refreshing the ListView, I have come up with a better solution which reduces size of code to an extent. I have created a new method refreshListView() which is called every time I want to refresh the ListView.

......
......
public void onClick(
        DialogInterface dialogInterface, int i) {
    try {
        Runtime.getRuntime()
        .exec("/system/xbin/rm -rf "
                + selectedfolder
                .getAbsolutePath()
                .toString());
        refreshListView();

    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}
......
......
protected void refreshListView() {
    fsitem = NameManager.getfsiList();
    NameManager.WriteName(fsitem);
    MatrixCursor cursor = NameManager.getnameList();
    ListView list = (ListView) getListView();
    String[] from = { "name", "info", "status", "path", "folder", BaseColumns._ID };
    int[] to = { R.id.name, R.id.info, R.id.status, R.id.path };
    final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.row, cursor, from, to);
    list.setAdapter(adapter);

}
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134