gpt4 book ai didi

android - 在 Android 的 ListView CheckBox 中设置 SelectedValue

转载 作者:行者123 更新时间:2023-11-30 02:56:29 24 4
gpt4 key购买 nike

我在 Android 的 CheckBox 中使用 ListView。它工作正常。

现在,我想根据参数在 CheckBox 中设置 Selected Value。因此在加载列表时,如果 Parameter User 为 Yes,则检查 Checkbox 否则不检查。在这里,在下面基于 country.getUser() 的代码中,我想检查 CheckBox。来源:ListView CheckBox in Android

我的代码:

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();
country.setSelected(cb.isChecked());
}
});
}
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;

}
}

请帮我解决这个问题。

最佳答案

您可以使用 setChecked 来检查复选框取决于这样的值

if(country.getUser().equalsIgnoreCase("Yes")){
holder.checkBox.setChecked(true);
}
else {
holder.checkBox.setChecked(false);
}

关于android - 在 Android 的 ListView CheckBox 中设置 SelectedValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23172676/

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