如何将选中的复选框android转换为数组

时间:2014-05-10 10:10:00

标签: android arrays checkbox

我有一个问题,当我选中复选框然后单击一个按钮时,复选框的选定值显示在Toast中。但我想将选定的值保存到数组。谁能帮我 ? 这是我的代码

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

1 个答案:

答案 0 :(得分: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);