Android - 带复选框的 ListView

标签 android listview checkbox

我已经实现了一个带有复选框的 ListView ,只要用户单击该复选框,该复选框就会被选中。但我意识到这种方式非常不方便,我想将其更改为每当用户单击一行时,复选框就会自动勾选。我的问题是我该怎么做?如何更改 listView.setOnItemClickListener 中的代码以使我的复选框被选中?有什么想法吗?

这是我到目前为止的代码:

MyCustomAdapter dataAdapter = null;
    public static ArrayList countries = new ArrayList();;

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

            ArrayList countries = new ArrayList();
            countries.clear(); // refresh

            //Generate list View from ArrayList
            displayListView();

            Button btn1 = (Button) findViewById(R.id.next);

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    startActivity(new Intent(ShowCountries.this, ShowTypes.class));
                }
            });
        }
        else
        {
            startActivity(new Intent(ShowCountries.this, NoInternetConnection.class));
        }

    }

    private void displayListView() {

        //Array list of countries
        countries.clear();//refresh
        ArrayList<Country> countryList = new ArrayList<Country>();
        Country country = new Country("POIs","Show POIs around me",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.list_countries);
        // 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);
            if (countries.indexOf(country.getName()) !=-1) {
                countries.remove(country.getName());
            }
            else
            {
                // thick checkbox
                countries.add(country.getName());
            }
              //  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);

                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();
                        if (countries.indexOf(cb.getText()) !=-1) {
                            country.setSelected(cb.isChecked());
                            countries.remove(cb.getText());
                        }
                        else
                        {
                            countries.add(cb.getText());
                        }
                    }
                });
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }

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

            return convertView;
        }

    }
}

最佳答案

我想有几种方法可以做到这一点,我使用的是: 使用具有 Checkbox 的自定义 View 创建自定义 ListView,然后在 getView() 方法中使用该 Checkbox。然后让它实现setOnCheckChangeListener()来注册点击。

并且不要忘记将值设置为 Checkbox 例如

Checkbox.setChecked(condition)
Checkbox.setOnCheckChangeListener()

希望这有帮助

关于Android - 带复选框的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31944373/

相关文章:

android - flutter 错误 : 'ScrollController not attached to any scroll views.' on scroll

android - 如何同时为两个应用程序使用同一个 USB 配件?

c# - 以编程方式将列添加到 WPF 中的 ListView ?

javascript - Bootstrap 复选框切换 - 单击后如何获取复选框的值

html - 使上一个/下一个按钮在 iOS 上的复选框上起作用

jquery - 如何为复选框标签添加突出显示?

android - 如何在 Volley 中获取 JSONObject 服务器响应

java - 尝试解析 JSON 数据时出现此错误 W/System.err : org. json.JSONException:对客户没有值(value)

android listview下拉创建新行

android - JSON 解析成 ListView