1

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();    
 }  
4

1 回答 1

1

尝试调用:

adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.notifyDataSetChanged();
于 2013-10-08T19:29:15.893 回答