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;
您可以调用列表视图的适配器的notifyDataSetChanged()或notifyDataSetInvalidated()
Since I have like 8 other AlertDialog
s 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.