游标仅返回一个值/最后一个值

时间:2018-02-09 05:48:43

标签: android arrays sqlite android-cursor

我真的被卡住,因为我有一个从数据库中检索值的游标,但游标只返回最后一个值。我需要光标来检索所有值,以便稍后我可以显示它们。有什么办法可以返回通过游标存储的所有数据吗?

关于发生什么的例子。例如,单击按钮1并完美存储1,但是一旦我点击按钮2并添加1.按钮1数据不会返回或检索。

非常感谢任何帮助。

执行

第一个Cursor将检查所有存储的项目。

public void exectute() {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            Cursor c = TrackerDb.getStoredItems(getApplicationContext());
            if (c != null) {
                if (c.moveToFirst()) {
                    WorkoutDetails details = null;
                    //if (details != null) mWorkoutDetailsList.add(details);
                    do {
                        //if (details != null) mWorkoutDetailsList.add(details);
                        WorkoutDetails temp = getWorkoutFromCursor(c);
                        //if (details != null) mWorkoutDetailsList.add(details);
                        if (details == null) {
                            details = temp;
                            continue;
                        }
                        //if (details != null) mWorkoutDetailsList.add(details);
                        if (isSameDay(details.getWorkoutDate(), temp.getWorkoutDate())) {
                            //if (details != null) mWorkoutDetailsList.add(details);
                            if (DBG) Log.d(LOG_TAG, "isSameDay().. true");
                            //details.add(temp);
                        } else {
                            mWorkoutDetailsList.add(details);
                            details = temp;
                        }
                       // if (details != null) mWorkoutDetailsList.add(details);
                    } while (c.moveToNext());
                    if (details != null) mWorkoutDetailsList.add(details);

                    if (DBG)
                        Log.d(LOG_TAG, "AsyncTask: list size " + mWorkoutDetailsList.size());

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mWorkoutsAdapter.updateList(mWorkoutDetailsList);
                        }
                    });
                }


                c.close();
            }
        }
    });
}

获取存储物品代码

这是excute类调用的代码,光标只返回一个值。

public static Cursor getStoredItems(Context context) {
    DBHelper dbHelper = new DBHelper(context);
    SQLiteDatabase db  = dbHelper.getWritableDatabase();
    String[] projection = {ID, TIME, TYPE, DURATION, DATE, POINT};
    String orderBy = TIME + " DESC";
    Cursor cursor = db.query(TABLE_NAME, projection, null, null, null, null, orderBy);
    return cursor;
}

阵列

这是数组代码,我希望光标根据类型存储其值。

private WorkoutDetails getWorkoutFromCursor(Cursor c) {
    long time = c.getLong(c.getColumnIndex(TrackerDb.TIME));
    int type = c.getInt(c.getColumnIndex(TrackerDb.TYPE));
    int duration = c.getInt(c.getColumnIndex(TrackerDb.DURATION));
    int point = c.getInt(c.getColumnIndex(TrackerDb.POINT));

    int totalMoney = MoneyActivity.Money.values().length;
    int[] points = new int[totalMoney];

    int totalActivities = MeditationTrackerActivity.ACTIVITIES.values().length;
    int[] durations = new int[totalActivities];
    if (type < totalActivities) {
        durations[type] = duration;
    }

    if( type == 0) {
        for (int i = 0; i < totalMoney; i++) {
            points[type] = point;
        }
    }
    else if ( type == 1) {
        for ( int ii = 0; ii < totalMoney; ii++) {
            points[type] = point;
        }
    }
    else if ( type == 2) {
        for ( int iii = 0; iii < totalMoney; iii++) {
            points[type] = point;
        }
    }

    return new WorkoutDetails(time, durations, points);
}

从光标代码中获取锻炼

private static WorkoutDetails getWorkoutFromCursor(Cursor c) {
    long time = c.getLong(c.getColumnIndex(TrackerDb.TIME));
    int type = c.getInt(c.getColumnIndex(TrackerDb.TYPE));
    int duration = c.getInt(c.getColumnIndex(TrackerDb.DURATION));
    String date = c.getString(c.getColumnIndex(TrackerDb.DATE));

    int point = c.getInt(c.getColumnIndex(TrackerDb.POINT));
    int[] durations = new int[MeditationTrackerActivity.ACTIVITIES.values().length];
    durations[type] = duration;

    int[] points = new int[MoneyActivity.Money.values().length];
    points[type] = point;
    return new WorkoutDetails(time, durations, date, points);
}

0 个答案:

没有答案