从sqlite读取数据时出现NullPointerException

时间:2014-11-18 16:58:35

标签: android sqlite listview

我有一个sqlite数据库和2个Listviews,假设A和B使用CursorAdapter填充。

在listview A中,我得到特定的行(假设列A的值为真的行),在列表视图B中,我得到列B的值为假的行。

第一个列表填充没有任何问题,但在第二个列表视图中,它给了我NPE;

archivetaskslist.setAdapter(adapter).

listview片段:

 tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(getActivity());
    final Cursor cursor = tasksDatabaseOperations.readData();
    taskslist = (SwipeListView) view.findViewById(R.id.tasks_list);
    adapter = new tasksCursorAdapter(getActivity(), cursor, CursorAdapter.FLAG_AUTO_REQUERY);
    taskslist.setAdapter(adapter);

listview的适配器A:

    public class tasksCursorAdapter extends CursorAdapter implements Serializable {

    Context context;
    View retView;
    Button delete_item, archive_item;

    public tasksCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        this.context = context;

    }

    @Override
    public View newView(final Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        retView = inflater.inflate(R.layout.single_row, parent, false);
        YoYo.with(Techniques.ZoomIn).duration(700).playOn(retView);
        return retView;
    }

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {
        final tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(context);
        final View getview = view;
        tasksDatabaseOperations.getWritableDatabase();
        TextView tvTask, tvDate, tvTime, tvRepeat;
        tvRepeat = (TextView)view.findViewById(R.id.repeat_added);
        tvTask = (TextView) view.findViewById(R.id.task_added);
        tvDate = (TextView) view.findViewById(R.id.date_added);
        tvTime = (TextView) view.findViewById(R.id.time_added);
        tvTask.setText(cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_TASK)));
        tvDate.setText("On " + cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_DATE)));
        tvTime.setText("@ " + cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_TIME)));
        String repeat_after = cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_TASK_REPEAT_AFTER));
        String every = cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_REPEAT_EVERY));
        String true_false = cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_TASK_TRUE_FALSE));
        Log.d("Temp", true_false);

        if (repeat_after.equals("")){
            tvRepeat.setVisibility(View.GONE);
        }
        else {
            tvRepeat.setVisibility(View.VISIBLE);
            tvRepeat.setText("Repeat every" + " " + repeat_after + " " + every);
        }
        final int pos = cursor.getPosition();
        final String row_id = cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.UID));

}

SQLiteHelper类中的readData:

    public Cursor readData() {
    String[] allColumns = new String[] {UID, ASSIS_TASK, ASSIS_DATE, ASSIS_TIME, ASSIS_TASK_REPEAT_AFTER, ASSIS_TASK_TRUE_FALSE, ASSIS_REPEAT_EVERY};
    Cursor c =sqLiteDatabase.query(ASSIS_TASK_TABLE_NAME, allColumns, ASSIS_TASK_TRUE_FALSE + "='" + "false'",null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

这里的一切都很好。列表视图A中的数据会根据需要添加。

listview B:

 tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(this);
    final Cursor cursor = tasksDatabaseOperations.readArchivedData();
    archivetaskslist = (SwipeListView)findViewById(R.id.archive_tasks_list);
    adapter = new archiveTasksAdapter(this, cursor, CursorAdapter.FLAG_AUTO_REQUERY);
    archivetaskslist.setAdapter(adapter);

listview B的适配器:

 Context context;
    View ar_retView;

    public archiveTasksAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        this.context = context;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        ar_retView = inflater.inflate(R.layout.archived_task_single_row, parent, false);
        YoYo.with(Techniques.ZoomIn).duration(700).playOn(ar_retView);
        return ar_retView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final tasks_Database_Operations tasksDatabaseOperations = new tasks_Database_Operations(context);
        tasksDatabaseOperations.getWritableDatabase();
        final View bView = view;
        TextView atvTask, atvDate, atvTime, atvRepeat;
        atvTask = (TextView)view.findViewById(R.id.archive_task_added);
        atvDate = (TextView)view.findViewById(R.id.archive_date_added);
        atvTime = (TextView)view.findViewById(R.id.archive_time_added);
        atvRepeat = (TextView)view.findViewById(R.id.archive_repeat_added);
        atvTask.setText(cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_TASK)));
        atvDate.setText("On " + cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_DATE)));
        atvTime.setText("@ " + cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_TIME)));
        String repeat_after = cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_TASK_REPEAT_AFTER));
        String every = cursor.getString(cursor.getColumnIndex(tasksDatabaseOperations.ASSIS_REPEAT_EVERY));
}

这是用于读取listview B的数据的SQLiteHlper方法:

    public Cursor readArchivedData() {
    String[] allColumns = new String[] {UID, ASSIS_TASK, ASSIS_DATE, ASSIS_TIME, ASSIS_TASK_REPEAT_AFTER, ASSIS_TASK_TRUE_FALSE, ASSIS_REPEAT_EVERY};
    Cursor c =sqLiteDatabase.query(ASSIS_TASK_TABLE_NAME, allColumns, ASSIS_TASK_TRUE_FALSE + "='" + "true'",null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;

0 个答案:

没有答案