Question

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();
        }
    });
Was it helpful?

Solution

// 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top