I have an array adapter that displays a list if items. I am creating a list of items for user to select in a dialog and when the user selects the items from that list and when he clicks "Ok " button on the dialog box I want the array adapter to be updated. The dialog box is created when a user clicks a button. To do this I first created a list and an array adapter. Now the question is that the list never gets updated when I select OK. It never gets notified on change. What am I missing here.
ListView listView = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> adapter;
ArrayList<String> list = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
mchooseItems.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
launchAddItems();
}
});
listView.setAdapter(adapter);
protected void launchAddItems(){
AlertDialog.Builder ab=new AlertDialog.Builder(this);
ab.setTitle("Select Items")
.setMultiChoiceItems(items_new, null, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
String checked_item = mitems.get(which);
if (isChecked){
mSelectedItems.add(checked_item);
}
else if(mSelectedItems.contains(which)){
mSelectedItems.remove(Integer.valueOf(checked_item));
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
list.add("1");
list.add("2");
list.add("3");
adapter.notifyDataSetChanged();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mSelectedItems.clear();;
}
});
ab.create();;
ab.show();
}