当用户更改复选框时,我需要在列表视图中保存该项目。
我有一个自定义基础适配器,带有我的列表项的视图持有者。
我可以根据复选框将对象状态设置为true和false,但我不确定如何保存数据。
我使用此代码获取nullpointer异常。我认为是因为我试图调用我的DatabaseHandler类。
如何从这里获取数据到SQLite的任何指针将不胜感激。
我的自定义基本适配器
public class ChecklistBaseAdapter extends BaseAdapter {
private static ArrayList<Checklist> searchArrayList;
Context currentcontext;
DatabaseHandler db = new DatabaseHandler(currentcontext);
private LayoutInflater mInflater;
public ChecklistBaseAdapter(Context context, ArrayList<Checklist> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public void remove(int position) {
searchArrayList.remove(position);
notifyDataSetChanged();
}
public void removeAll() {
searchArrayList.clear();
notifyDataSetChanged();
}
public void notifyChange() {
notifyDataSetChanged();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.checklistitem, null);
holder = new ViewHolder();
holder.cbStatus = (CheckBox) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.cbStatus.setText(searchArrayList.get(position).getName());
if (searchArrayList.get(position).getStatus().equals("F")) {
holder.cbStatus.setChecked(false);
} else {
holder.cbStatus.setChecked(true);
}
holder.cbStatus
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
String name = holder.cbStatus.getText().toString();
if (isChecked) {
for (int x = 0; x < searchArrayList.size(); x++) {
Checklist checklist = new Checklist();
checklist = searchArrayList.get(x);
if (name.equals(checklist.getName())) {
checklist.setStatus("T");
db.updateCheckList(checklist);
searchArrayList.set(x, checklist);
}
}
} else {
for (int x = 0; x < searchArrayList.size(); x++) {
Checklist checklist = new Checklist();
checklist = searchArrayList.get(x);
if (name.equals(checklist.getName())) {
checklist.setStatus("F");
searchArrayList.set(x, checklist);
db.updateCheckList(checklist);
}
}
}
}
});
return convertView;
}
static class ViewHolder {
CheckBox cbStatus;
}
我的数据库处理程序类
中的更新方法public int updateCheckList(Checklist checklist) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CHECK_NOTE_GUID, checklist.getNoteguid());
values.put(CHECK_KEY_NAME, checklist.getName());
values.put(CHECK_KEY_STATUS, checklist.getStatus());
values.put(CHECK_KEY_GUID, checklist.getGuid());
// updating row
db.update(TABLE_CHECKLIST, values, CHECK_KEY_ID + " = ?",
new String[] { String.valueOf(checklist.getID()) });
db.close();
return 1;
}
答案 0 :(得分:0)
当currentContext仍然为null时,您正在创建DatabaseHandler。放行
DatabaseHandler db = new DatabaseHandler(currentcontext);
进入类的构造函数并使用传递的上下文而不是currentContext或事先设置currentContext:
public ChecklistBaseAdapter(Context context, ArrayList<Checklist> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
currentContext = context;
DatabaseHandler db = new DatabaseHandler(currentContext);
}