1

i have a problem, when i checked checkbox then click a button, the selected value of checkbox show in Toast. But i want to save the selected value to array. Can anyone help me ? Here is my code

    submit = (Button) findViewById(R.id.submit);
    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            StringBuffer responseText = new StringBuffer();
            responseText.append("The following were selected...\n");

            ArrayList<Planet> planetList = listAdapter.planetList;
            for (int i = 0; i < planetList.size(); i++) {
                Planet planet = planetList.get(i);
                if (planet.isChecked()) {

                    responseText.append("\n" + planet.getName());
                }
            }

            Toast.makeText(getApplicationContext(), responseText,
                    Toast.LENGTH_LONG).show();
        }
    });
Noerj
  • 13
  • 4

1 Answers1

0
// try this way,hope this will help you...

        ArrayList<String> valuesList = new ArrayList<String>();
        ArrayList<Planet> planetList = listAdapter.planetList;
        for (int i = 0; i < planetList.size(); i++) {
            Planet planet = planetList.get(i);
            if (planet.isChecked()) {
                valuesList.add(planet.getName());
                responseText.append("\n" + planet.getName());
            }
        }
        System.out.println("Selected Values List Size : "+valuesList.size());
        String[] valueArray = new String[valuesList.size()];
        for(int j=0;j<valuesList.size();j++){
            valueArray[j]=valuesList.get(j);
        }
        System.out.println("Selected Values Array Size : "+valueArray.length);
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • thank you, your code above works to get size of my checked checkbox. But i wanna get the value of my checked checkbox. In my code above when I click button, Toast shown "The following were selected Pluto, Mars". I wanna save "Pluto" and "Mars" into array not the size so i can insert the value into mysql. Can you help me to solve this ? Thanks before :) – Noerj May 10 '14 at 12:40
  • yes your checkbox value is already store in valueArray can you please try access this using loop valueArray. – Haresh Chhelana May 10 '14 at 12:47