gpt4 book ai didi

Android - 带复选框的 ListView

转载 作者:搜寻专家 更新时间:2023-11-01 07:53:11 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com