I have a dialog that contains a listview, the data of the list view comes from the parent of the dialog. Every time the dialog is about to be displayed, it should get data from the activity to build its list items. I know I should do this in onPrepareDialog(), but I don't know how to update the listview created by AlertDialog.builder, Could anyone help me?
My dialog is created from below code:
new AlertDialog.Builder(this)
.setTitle(title)
.setMultiChoiceItems(c开发者_C百科ityNames(), updateSelections(),
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
if(isChecked){
selections[whichButton] = true;
// Toast.makeText(MainActivity.this, cities.get(whichButton).getName(), Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
for(int i=0;i<selections.length;i++){
if(selections[i]==true){
removeCityFromScreen(i);
}
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
I am creating a custom adapter for listview..doing like this :
dialog2 = new Dialog(SActivity.this);
ListView modeList = new ListView(SActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(SActivity.this);
builder.setTitle(" resul[s] ");
MySimpleAdapter adapter = new MySimpleAdapter(SActivity.this, data , R.layout.list_main,
new String[] { "name", "distance" ,"phone","web"},
new int[] { R.id.item_title, R.id.item_subtitle ,R.id.item_subtitle1 ,R.id.item_subtitle2});
modeList.setAdapter(adapter);
builder.setView(modeList);
dialog2 = builder.create();
dialog2.show();
Simple answer: After calling create()
there's no chance to update the list. The builder inflates XML's and creates custom adapters when calling that. I would recommend to create / build the dialog every time you're about to display it.
Note: Af course you could provide a custom AlertDialog
setup with an own multi choice implementation. Then you could easily updated it since you have access to the adapter of the list (compare this answer - you could create and set a new adapter in this case).
精彩评论