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