如何在复选框中选择全部

时间:2015-05-15 09:43:37

标签: android

我有一个listview和一个复选框,如何在listview中给出SELECT ALL复选框,下面是我的getView()代码。有任何财产可以完成这项任务吗?请发送您对我当前代码的建议。谢谢。

代码:

 public class MainActivity extends Activity {

    MyCustomAdapter dataAdapter = null;
    private ArrayList<CheckBox> checkBoxes = new ArrayList<>();

    private String[] application = { "AA", "BB", "CC"}; // Dynamic Application Names
    private String[] device = { "EE", "FF", "GG"}; // Dynamic
                                                                                // Device
                                                                                // Names
    private RadioGroup radioGroup1;
    private RadioGroup radioGroup2;
    private RadioButton btn;
    private RadioButton btn2;
    private String text1;
    private String text2;
    RadioButton button1;
    RadioButton button2;
    Button selectall;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Generate list View from ArrayList
        displayListView();

        checkButtonClick();
        radioGroup1 = (RadioGroup) findViewById(R.id.radio1);
        radioGroup2 = (RadioGroup) findViewById(R.id.radio1);

        ViewGroup hourButtonLayout = (ViewGroup) findViewById(R.id.radio1); // This
                                                                            // is
                                                                            // the
                                                                            // id
                                                                            // of
                                                                            // the
                                                                            // RadioGroup
                                                                            // we
                                                                            // defined
        for (int i = 0; i < application.length; i++) {
            button1 = new RadioButton(this);
            button1.setId(i);
            button1.setText(application[i]);
            hourButtonLayout.addView(button1);

            radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                   public void onCheckedChanged(RadioGroup mRadioGroup2, int checkedId2) {
                        for(int i=0; i<mRadioGroup2.getChildCount(); i++) {
                             btn = (RadioButton) mRadioGroup2.getChildAt(i);
                             int t=mRadioGroup2.getId();
                             System.out.println(t);

                             if(btn.getId() == checkedId2) {
                                  text1 = btn.getText().toString();
                                  Toast.makeText(getApplicationContext(), "You selected : " + text1, Toast.LENGTH_SHORT).show();
                                  return;
                             }
                        }
                   }
              });

        }

        ViewGroup hourButtonLayout2 = (ViewGroup) findViewById(R.id.radio2); // This
                                                                                // is
                                                                                // the
                                                                                // id
                                                                                // of
                                                                                // the
                                                                                // RadioGroup
                                                                                // we
                                                                                // defined
        for (int i = 0; i < device.length; i++) {
            button2 = new RadioButton(this);
            button2.setId(i);
            button2.setText(device[i]);
            hourButtonLayout2.addView(button2);

            radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                   public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
                        for(int i=0; i<mRadioGroup.getChildCount(); i++) {
                             btn2 = (RadioButton) mRadioGroup.getChildAt(i);
                             int t=mRadioGroup.getId();
                             System.out.println(t);

                             if(btn2.getId() == checkedId) {
                                  text2 = btn2.getText().toString();
                                  Toast.makeText(getApplicationContext(), "You selected : " + text2, Toast.LENGTH_SHORT).show();
                                  return;
                             }
                        }
                   }
              });

        }

    }

    private void displayListView() {

        // Array list of countries
        ArrayList<Country> countryList = new ArrayList<Country>();
        Country country = new Country("", "Al Jazeera", false);
        countryList.add(country);
        country = new Country("", "AA", true);
        countryList.add(country);
        country = new Country("", "BB", false);
        countryList.add(country);


        // create an ArrayAdaptar from the String Array
        dataAdapter = new MyCustomAdapter(this, R.layout.country_info,
                countryList);
        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Country country = (Country) parent.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(),
                        "Clicked on Row: " + country.getName(),
                        Toast.LENGTH_LONG).show();
            }
        });

    }

    private class MyCustomAdapter extends ArrayAdapter<Country> {

        private ArrayList<Country> countryList;

        public MyCustomAdapter(Context context, int textViewResourceId,
                ArrayList<Country> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<Country>();
            this.countryList.addAll(countryList);
        }

        private class ViewHolder {
            TextView code;
            CheckBox name;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.country_info, null);

                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView
                        .findViewById(R.id.checkBox1);
                convertView.setTag(holder);

                checkBoxes.add(holder.name);

                selectall = (Button) findViewById(R.id.selectall);

                selectall.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        for (int i = 0; i < checkBoxes.size(); i++) {
                            CheckBox currentChecBox = checkBoxes.get(i); 
                            currentChecBox.setChecked(true);
                        }

                    }

                });

                holder.name.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Country country = (Country) cb.getTag();
                        Toast.makeText(
                                getApplicationContext(),
                                "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
                        country.setSelected(cb.isChecked());
                    }
                });
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Country country = countryList.get(position);
            holder.code.setText("");
            holder.name.setText(country.getName());
            holder.name.setChecked(country.isSelected());
            holder.name.setTag(country);

            return convertView;

        }

    }

    private void checkButtonClick() {

        Button myButton = (Button) findViewById(R.id.findSelected);
        myButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

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

                ArrayList<Country> countryList = dataAdapter.countryList;
                for (int i = 0; i < countryList.size(); i++) {
                    Country country = countryList.get(i);
                    if (country.isSelected()) {
                        responseText.append(", " + country.getName());
                        String response = responseText.toString();
                    }
                }

                // Get selected radiobuttons
                if(radioGroup1.getCheckedRadioButtonId()!=-1){
                   text1 = btn.getText().toString();
                   Log.d("Button", "Text 1 : " + text1);
                }

                if(radioGroup2.getCheckedRadioButtonId()!=-1){
                   text2 = btn2.getText().toString();
                   Log.d("Button", "Text 2 : " + text2);
                }

                Toast.makeText(getApplicationContext(), "SHOW NAMES : " + responseText + "and APPLICATION : " + text1 + "and DEVICE : " + text2, Toast.LENGTH_LONG).show();

            }
        });

    }

}

3 个答案:

答案 0 :(得分:1)

您可以创建一个包含所有复选框的数组列表,然后在您希望它们全部被选中时,只需在数组中执行for循环,并在每个复选框上执行setChecked(true)。例如

在适配器顶部声明arraylist

private ArrayList<CheckBox> checkBoxes = new ArrrayList<>();

然后在getView中添加数组的复选框。显然确保在实例化之后添加它。

checkBoxes.add(holder.name); 

然后在onClick

        for (int i = 0; i < checkBoxes.size(); i++) {
        CheckBox currentChecBox = checkBoxes.get(i); 
        currentChecBox.setChecked(true);
    }

答案 1 :(得分:0)

没有财产。

您需要跟踪所有复选框,当您点击全选时,请在每个复选框上调用setChecked(true)标记所有复选框。

答案 2 :(得分:0)

你可以实现它..使用.. 当您单击选择全部时,只需应用此代码

for (int i=0;i<your_list_view.getChildCount();i++) {
    View v = your_list_view.getChildAt(i);

    CheckBox cb = v.findViewById(R.id.your_checkbox_id);
    cb.setChecked(true);
}