Java 在Android中访问ListView复选框
我正在android上制作一个待办事项列表应用程序。除了在ListView中有无法以编程方式设置的复选框外,其他一切都运行得很好。我正在使用一个SQLite数据库来存储我的任务以及它们是否被标记为已完成。当我选中复选框时,它会成功地在我的数据库中保存一个“1”。我有一个updateUI方法,可以从数据库中读取任务的文本并显示它。我创建了一个新的“updateUICheckBox”方法来处理复选框的UI更新。现在我只是想把我所有的复选框都设置为选中,但是当我调用setChecked(true)时,我得到了一个nullpointerexceptionJava 在Android中访问ListView复选框,java,android,android-layout,listview,checkbox,Java,Android,Android Layout,Listview,Checkbox,我正在android上制作一个待办事项列表应用程序。除了在ListView中有无法以编程方式设置的复选框外,其他一切都运行得很好。我正在使用一个SQLite数据库来存储我的任务以及它们是否被标记为已完成。当我选中复选框时,它会成功地在我的数据库中保存一个“1”。我有一个updateUI方法,可以从数据库中读取任务的文本并显示它。我创建了一个新的“updateUICheckBox”方法来处理复选框的UI更新。现在我只是想把我所有的复选框都设置为选中,但是当我调用setChecked(true)时,
由于要设置复选框的是ListView项,因此必须在ListView本身的适配器中设置它。并操纵数据模型以选中或取消选中复选框
通过扩展用于填充ListView的BaseAdapter创建新类。如果要管理的自定义布局包含多个UI对象,请不要使用ArrayAdapter尝试在列表中添加标志
public class Country {
public String name;
public String code;
public String abbreviation;
public boolean selected = false; //--> example this flag
public int image;
}
适配器
public class CountryCodeAdapter extends BaseAdapter {
private Context context;
private List<Country> countryList;
public CountryCodeAdapter(Context context, List<Country> countryList) {
this.context = context;
this.countryList = countryList;
}
@Override
public int getCount() {
return countryList.size();
}
@Override
public Object getItem(int position) {
return countryList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.adapter_country_code, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
viewHolder.tv_item_country_name.setText(country.getName());
String plus = context.getResources().getString(R.string.plus);
viewHolder.tv_item_country_code.setText(plus.concat(country.getCode()));
int image = country.getImage();
if (image != -1) {
viewHolder.iv_item_country.setImageResource(image);
}
return convertView;
}
public void updateList(List<Country> countryList) {
this.countryList = countryList;
notifyDataSetChanged();
}
static class ViewHolder {
@Bind(R.id.iv_item_country)
ImageView iv_item_country;
@Bind(R.id.tv_item_country_name)
TextView tv_item_country_name;
@Bind(R.id.tv_item_country_code)
TextView tv_item_country_code;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
使用此标志在适配器内设置复选框。我添加了一个扩展BaseAdapter的内部类(请参见上面的编辑)。我填写了getView方法。我如何在updateUICheckBox方法中利用它根据数据库值设置复选框?请检查此实现
public class CountryCodeAdapter extends BaseAdapter {
private Context context;
private List<Country> countryList;
public CountryCodeAdapter(Context context, List<Country> countryList) {
this.context = context;
this.countryList = countryList;
}
@Override
public int getCount() {
return countryList.size();
}
@Override
public Object getItem(int position) {
return countryList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.adapter_country_code, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
viewHolder.tv_item_country_name.setText(country.getName());
String plus = context.getResources().getString(R.string.plus);
viewHolder.tv_item_country_code.setText(plus.concat(country.getCode()));
int image = country.getImage();
if (image != -1) {
viewHolder.iv_item_country.setImageResource(image);
}
return convertView;
}
public void updateList(List<Country> countryList) {
this.countryList = countryList;
notifyDataSetChanged();
}
static class ViewHolder {
@Bind(R.id.iv_item_country)
ImageView iv_item_country;
@Bind(R.id.tv_item_country_name)
TextView tv_item_country_name;
@Bind(R.id.tv_item_country_code)
TextView tv_item_country_code;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
CountryCodeAdapter adapter = new CountryCodeAdapter(activity, countryList);
//eg. you may change the 1st element in countryList selected flag into true and //call updateList
if (adapter != null) {
adapter.updateList(countryList);
}