1

当用户更改复选框时,我需要将项目保存在列表视图中。

我有一个自定义基本适配器,其中包含用于列表项的视图支架。

我可以根据复选框将对象状态设置为 true 和 false,但我不确定如何保存数据。

这段代码我得到空指针异常。我认为这是因为我试图调用我的 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;
}
4

1 回答 1

0

当 currentContext 仍然为空时,您正在创建 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);
}
于 2013-03-13T12:22:14.860 回答