简体   繁体   English

游标仅返回一个值,而不是所有值

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

I have encountered a problem in my Android application. 我的Android应用程序遇到了问题。 I want to retrieve all values of a column, but I am only getting one of the values. 我想检索列的所有值,但是我只得到其中一个值。 I have tried everything, but I could still not find a solution. 我已经尝试了所有方法,但是仍然找不到解决方案。 Here is my code in my database: 这是我数据库中的代码:

 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;
}

Here is my code in another class where it shows a toast with all the values. 这是我在另一个类中的代码,其中显示了带有所有值的吐司。

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();

All I am getting is Protein = 0 . 我得到的只是Protein = 0 What I should be getting is 0, 0, 0, 0, 0, 0,... . 我应该得到的是0, 0, 0, 0, 0, 0,... I don't know what I am doing wrong here. 我不知道我在做什么错。 In my ListView, I got it right. 在我的ListView中,我做对了。 It worked perfectly there. 它在那里完美工作。 Here is my ListView code: 这是我的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"));

And that returns a Toast with a bunch of values. 然后返回带有一堆值的Toast。 Any help regarding this problem would be appreciated. 任何有关此问题的帮助将不胜感激。

You need to loop over the cursor if the cursor is returning more than one row, and build your output in the loop. 如果游标返回的行多于一行,则需要遍历游标,并在循环中构建输出。

Keep calling c.moveToNext() until it returns false. 继续调用c.moveToNext()直到返回false。

In getAllColumns() , your query statement is correct. getAllColumns() ,您的查询语句正确。 It does in fact fetch all columns. 实际上,它确实获取所有列。 The if statement that follows simply points it to the first result. 后面的if语句只是将其指向第一个结果。 Then you are returning the cursor while it is pointed to the first result. 然后,您将光标指向第一个结果时返回。 You should take out the if statement because it doesn't do anything useful. 您应该取出if语句,因为它没有任何用处。

When trying to show the toast, you want something like this: 在尝试烤面包时,您需要这样的东西:

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();

I had the same problem until I did this: 在执行此操作之前,我遇到了同样的问题:

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();
}

The cursor.moveToNext() will never check the first element. cursor.moveToNext()将永远不会检查第一个元素。 So I check in the course is not in the last one and then I add the object and move to the next one. 因此,我检查课程是否不在最后一个课程中,然后添加对象并移至下一个课程。

That's it! 而已! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM