0

I have an Array of strings in a ListView that is set to take multiple choices with "Done" being checked when the user is done. I'd like to create a new Activity using an Intent

String[] names = new String[] {"Ham","Cheese","Lettuce", "Bacon", "Done"};
    setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_multiple_choice,
                    android.R.id.text1, names));
    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

What I am trying to do below is based on the information that is checked by the user I want to put certain information into the new listview and then show them the listview once they click "Done" by startActivity. I'm not sure if this is the correct way to go about this.

Intent e = new Intent(getApplicationContext(), FormedList.class);
      if(listView.getCheckItemIds().toString().equals("Ham"))
          e.putExtra("Meat", selectedChildren);
      if(listView.getCheckItemIds().toString().equals("Cheese"))
          e.putExtra("Dairy", selectedChildren);
      if(listView.getCheckItemIds().toString().equals("Bacon"))
          e.putExtra("Swine", selectedChildren);
      if(listView.getCheckItemIds().toString().equals("Done"))
           startActivity(e);

1 Answer 1

1

Like in this example,

 ArrayList<Integer> extra_array = new ArrayList<Integer>();
     for (long i : mContactList.getCheckedItemIds()) {

    Log.v(TAG, "getCheckItemIds(): id = " + i);
    extra_array.add((int) i);
  }

The array contains the indexes of the selected items. You can put these items in a String array and pass it as an intent to the next activity.

More explanation : getCheckedItemIds() returns an array long [] containing the positions(number) of the checked items. You should loop through the array and use the indexes to select the checked items from names

1
  • //new listview with selected elements ArrayList<String> selectedChildren = new ArrayList<String>(); //Create an Intent for the next class Intent formed = new Intent(this, FormedList.class); for(long i : listView.getCheckItemIds() ) { selectedChildren.add((int) i, null); Log.v(TAG, "getCheckItemIds(): id = " + i); if(selectedChildren.equals("Done")) startActivity(formed); } @Reno I'm not sure if this is correct but I'm going of of what you mentioned. I get no errors but when i hit the "Done" option the new activity does not start.
    – RIVERE
    Nov 30, 2011 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.