Android-带复选框的Listview

Android-带复选框的Listview,android,listview,checkbox,Android,Listview,Checkbox,我实现了一个带有复选框的listview,每当用户单击它时,复选框都会被勾选。但是我意识到这样做很不方便,我想把它改为每当用户点击一行时,复选框就会自动勾选。我的问题是我怎么做?如何更改listView.setOnItemClickListener中的代码,以便勾选我的复选框?有什么想法吗 这是我目前的代码: MyCustomAdapter dataAdapter = null; public static ArrayList countries = new ArrayList();;

我实现了一个带有复选框的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;
        }

    }
}
MyCustomAdapter dataAdapter=null;
public static ArrayList countries=new ArrayList();;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if(isNetworkAvailable()){
setContentView(R.layout.activity\u show\u countries);
ArrayList国家/地区=新的ArrayList();
countries.clear();//刷新
//从ArrayList生成列表视图
displayListView();
按钮btn1=(按钮)findViewById(R.id.next);
btn1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startActivity(新意图(ShowCountries.this、ShowTypes.class));
}
});
}
其他的
{
startActivity(新意图(ShowCountries.this,NoInternetConnection.class));
}
}
私有void displayListView(){
//国家列表
countries.clear();//刷新
ArrayList countryList=新的ArrayList();
Country Country=新国家(“POI”,“显示我周围的POI”,false);
国家列表。添加(国家);
//从字符串数组创建ArrayAdaptar
dataAdapter=新的MyCustomAdapter(此,
R.layout.country_info,countryList);
ListView ListView=(ListView)findViewById(R.id.list\u国家);
//将适配器分配给ListView
setAdapter(dataAdapter);
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//单击后,显示带有文本视图文本的祝酒词
Country Country=(国家)父项.getItemAtPosition(位置);
if(countries.indexOf(country.getName())!=-1){
countries.remove(country.getName());
}
其他的
{
//厚复选框
countries.add(country.getName());
}
//Toast.makeText(getApplicationContext(),
//单击行:“+country.getName(),
//Toast.LENGTH_LONG).show();
}
});
}
私有类MyCustomAdapter扩展了ArrayAdapter{
私有ArrayList国家列表;
公共MyCustomAdapter(上下文,int textViewResourceId,
ArrayList国家列表){
super(上下文、textViewResourceId、countryList);
this.countryList=新的ArrayList();
this.countryList.addAll(countryList);
}
私有类视窗持有者{
文本视图代码;
复选框名称;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
Log.v(“ConvertView”,String.valueOf(position));
if(convertView==null){
LayoutInflater vi=(LayoutInflater)getSystemService(
上下文。布局(充气机和服务);
convertView=vi.充气(R.布局.国家/地区信息,空);
holder=新的ViewHolder();
holder.code=(TextView)convertView.findViewById(R.id.code);
holder.name=(复选框)convertView.findViewById(R.id.checkBox1);
convertView.setTag(支架);
holder.name.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
复选框cb=(复选框)v;
Country Country=(Country)cb.getTag();
Toast.makeText(getApplicationContext(),
单击复选框:“+cb.getText()+
“is”+cb.isChecked(),
Toast.LENGTH_LONG).show();
if(countries.indexOf(cb.getText())!=-1){
country.setSelected(cb.isChecked());
countries.remove(cb.getText());
}
其他的
{
countries.add(cb.getText());
}
}
});
}
否则{
holder=(ViewHolder)convertView.getTag();
}
Country=countryList.get(位置);
holder.code.setText(“+country.getCode()+”);
holder.name.setText(country.getName());
holder.name.setChecked(country.isSelected());
持有人名称、setTag(国家);
返回视图;
}
}
}

我想有两种方法可以做到这一点,我使用的方法是: 使用带有
复选框的自定义视图创建一个自定义
列表视图
,然后在
getView()
方法中使用该
复选框
。然后使其实现
setOnCheckChangeListener()
以注册单击

另外,例如,不要忘记将值设置为
复选框

Checkbox.setChecked(condition)
Checkbox.setOnCheckChangeListener()

我希望这有助于

单击列表项后,只需调用复选框上的toggle()函数即可。为您的listview注册一个listselector,以指示聚焦和按下的状态。

查看一些教程:或