簡體   English   中英

游標僅返回一個值,而不是所有值

[英]Cursor is only returning one value, not all values

我的Android應用程序遇到了問題。 我想檢索列的所有值,但是我只得到其中一個值。 我已經嘗試了所有方法,但是仍然找不到解決方案。 這是我數據庫中的代碼:

 public Cursor returnAllColumns() {
    Cursor cursor = mDb.query(FTS_VIRTUAL_TABLE,
            new String[] {KEY_ROWID,
                    KEY_NAME,
            KEY_CUSTOMER, PROTEIN, TOTAL_CARB}
            , null, null, null, null, null);
if (cursor != null) {
        cursor.moveToNext();
}
    return cursor;
}

這是我在另一個類中的代碼,其中顯示了帶有所有值的吐司。

mDbHelper = new DBAdapter(DatabaseFiller.this);
                    mDbHelper.open();
                    Cursor c = mDbHelper.returnAllColumns();

                    String protein = c.getString(c.getColumnIndexOrThrow("protein"));

                    Toast.makeText(getApplicationContext(), protein, Toast.LENGTH_SHORT).show();

我得到的只是Protein = 0 我應該得到的是0, 0, 0, 0, 0, 0,... 我不知道我在做什么錯。 在我的ListView中,我做對了。 它在那里完美工作。 這是我的ListView代碼:

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                cursor = (Cursor) mListView.getItemAtPosition(position);
                //mListView.setItemChecked(position, true);
                // Get the state's capital from this row in the database.
                String name = cursor.getString(cursor.getColumnIndexOrThrow("customer"));
                String caloriescursor = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                String totalfat = cursor.getString(cursor.getColumnIndexOrThrow("address"));
                String satfatcursor = cursor.getString(cursor.getColumnIndexOrThrow("city"));
                String state = cursor.getString(cursor.getColumnIndexOrThrow("state"));
                String zipCode = cursor.getString(cursor.getColumnIndexOrThrow("zipCode"));

然后返回帶有一堆值的Toast。 任何有關此問題的幫助將不勝感激。

如果游標返回的行多於一行,則需要遍歷游標,並在循環中構建輸出。

繼續調用c.moveToNext()直到返回false。

getAllColumns() ,您的查詢語句正確。 實際上,它確實獲取所有列。 后面的if語句只是將其指向第一個結果。 然后,您將光標指向第一個結果時返回。 您應該取出if語句,因為它沒有任何用處。

在嘗試烤面包時,您需要這樣的東西:

Cursor c = mDbHelper.returnAllColumns();
ArrayList<String> protein = new ArrayList<>();
while (c.moveToNext()) {
    protein.add(c.getString(c.getColumnIndexOrThrow("protein")));
}
Toast.makeText(getApplicationContext(), TextUtils.join(", ", protein), Toast.LENGTH_SHORT).show();

在執行此操作之前,我遇到了同樣的問題:

if (!cursor.moveToFirst()) {
    Log.d(LOG_TAG, "=== cursor not moveToFirst() ===");

    return null;
}

Log.d(LOG_TAG, "=== reading cursor of getAll() ===");
Log.d(LOG_TAG, String.valueOf(cursor));

List<Trip> trips = new ArrayList<>();
while (!cursor.isAfterLast()) {
    trips.add(cursorToTrip(cursor));

    cursor.moveToNext();
}

cursor.moveToNext()將永遠不會檢查第一個元素。 因此,我檢查課程是否不在最后一個課程中,然后添加對象並移至下一個課程。

而已! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM