Java Android使用光标在记录集中移动,不返回所有值

Java Android使用光标在记录集中移动,不返回所有值,java,android,sqlite,android-sqlite,Java,Android,Sqlite,Android Sqlite,我正在Android Studio中学习SQLite,在将数据从光标输出到TextView控件时遇到了一些问题。基本上,我有一个应用程序,它有一个插入按钮,静态插入6条记录。单击“显示当前记录”按钮时,我希望根据SQLite数据库中3列的内容更改三个文本视图的文本。然而,我当前的代码似乎只返回了三个结果,并且宠物类型没有填充 main活动 package com.example.devonryder.week9; import android.content.ContentValues; im

我正在Android Studio中学习SQLite,在将数据从光标输出到TextView控件时遇到了一些问题。基本上,我有一个应用程序,它有一个插入按钮,静态插入6条记录。单击“显示当前记录”按钮时,我希望根据SQLite数据库中3列的内容更改三个文本视图的文本。然而,我当前的代码似乎只返回了三个结果,并且宠物类型没有填充

main活动

package com.example.devonryder.week9;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Helper mHelper;
    Cursor cursor;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHelper = new Helper(this);

        // Gets the data repository in write mode

        db = mHelper.getWritableDatabase();
        cursor = UpdateCursor();

    }


    public void Insert(View view)
    {
        // Create a new map of values, where column names are the keys
        ContentValues values = new ContentValues();

        // Insert values
        values.put(Contract.dbEntry.COLUMN_OWNER, "Devon");
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "woofy");
        values.put(Contract.dbEntry.COLUMN_PET_AGE, 11);
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "doge");

        // Insert the new row, returning the primary key value of the new row
        long newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);

        // Clear the insert values
        values.clear();

        // Insert more values
        values.put(Contract.dbEntry.COLUMN_OWNER, "Steve");
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "fluff");
        values.put(Contract.dbEntry.COLUMN_PET_AGE, 12);
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "kitty");

        newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);

        values.clear();

        // Insert more values
        values.put(Contract.dbEntry.COLUMN_OWNER, "Dan");
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "jimmy boy");
        values.put(Contract.dbEntry.COLUMN_PET_AGE, 1);
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "lizard");

        newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);

        values.clear();

        // Insert more values
        values.put(Contract.dbEntry.COLUMN_OWNER, "James");
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "axle");
        values.put(Contract.dbEntry.COLUMN_PET_AGE, 3);
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "Tiger");

        newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);

        values.clear();

        // Insert more values
        values.put(Contract.dbEntry.COLUMN_OWNER, "Rick");
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "max");
        values.put(Contract.dbEntry.COLUMN_PET_AGE, 7);
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "dog");

        newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);

        values.clear();

        // Insert more values
        values.put(Contract.dbEntry.COLUMN_OWNER, "David");
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "slither");
        values.put(Contract.dbEntry.COLUMN_PET_AGE, 2);
        values.put(Contract.dbEntry.COLUMN_PET_NAME, "snake");

        newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);

        values.clear();

        // Set output
        TextView output = (TextView)findViewById(R.id.tvInsertOutput);
        output.setText("Just input " + newRowId);

        cursor = UpdateCursor();

    }

        private Cursor UpdateCursor()
    {
        String columns[] = {"owner", "PetName", "PetType"};
        String where = null;
        String groupBy = null;
        String having = null;
        String orderBy = null;
        String limit = null;

        cursor = db.query(Contract.dbEntry.TABLE_NAME,
                            null,
                            where,
                            null,
                            groupBy,
                            having,
                            orderBy,
                            limit);

        // Return Cursor
        return cursor;
    }

    public void DisplayCurrentRecord(View view)
    {
        String temp;
        final String EOF = "<eof>";
        TextView tempOwner = (TextView)findViewById(R.id.tvOwnerOutput);
        TextView tempPetName = (TextView)findViewById(R.id.tvPetNameOutput);
        TextView tempPetType = (TextView)findViewById(R.id.tvPetTypeOutput);

        if (cursor.isBeforeFirst()) { cursor.moveToNext(); }

        if (cursor != null)
        {
            cursor.moveToNext();

            // While the cursor is not after the end of the result set
            if (cursor.isAfterLast())
            {
                tempOwner.setText(EOF);
                tempPetName.setText(EOF);
                tempPetType.setText(EOF);
            }
            else
            {
                temp = cursor.getString(cursor.getColumnIndex(Contract.dbEntry.COLUMN_OWNER));
                tempOwner.setText(temp);
                temp = cursor.getString(cursor.getColumnIndex(Contract.dbEntry.COLUMN_PET_NAME));
                tempPetName.setText(temp);
                temp = cursor.getString(cursor.getColumnIndex(Contract.dbEntry.COLUMN_PET_TYPE));
                tempPetType.setText(temp);

                cursor.moveToNext();
            }
        }


    }

    public void ResetRecordSet(View view)
    {
        cursor.moveToFirst();
    }
}
助手

package com.example.devonryder.week9;

import android.content.Context;
import android.database.sqlite.*;

/**
 * Created by Devon Ryder on 3/8/2017.
 */
public class Helper extends SQLiteOpenHelper {
    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "pets.db";

    // Constructor -- Calls base class constructor
    public Helper(Context context) {
        super(context, DATABASE_NAME, null , DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }

    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + Contract.dbEntry.TABLE_NAME + " (" +
                    Contract.dbEntry._ID + " INTEGER PRIMARY KEY," +
                    Contract.dbEntry.COLUMN_OWNER + " TEXT," +
                    Contract.dbEntry.COLUMN_PET_NAME + " TEXT," +
                    Contract.dbEntry.COLUMN_PET_AGE + " INT," +
                    Contract.dbEntry.COLUMN_PET_TYPE + " TEXT)";

    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + Contract.dbEntry.TABLE_NAME;
}
XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.devonryder.week9.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:text="Insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="94dp"
        android:id="@+id/btnInsert"
        android:onClick="Insert" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="46dp"
        android:id="@+id/tvInsertOutput" />

    <TextView
        android:text="Owner name "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/btnInsert"
        android:layout_alignEnd="@+id/btnInsert"
        android:layout_marginBottom="85dp"
        android:id="@+id/tvOwner" />

    <TextView
        android:text="Pet Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tvOwner"
        android:layout_alignLeft="@+id/tvOwner"
        android:layout_alignStart="@+id/tvOwner"
        android:layout_marginTop="25dp"
        android:id="@+id/tvPetName" />

    <TextView
        android:text="Pet Type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:id="@+id/textView5"
        android:layout_below="@+id/tvPetName"
        android:layout_alignLeft="@+id/tvPetName"
        android:layout_alignStart="@+id/tvPetName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tvOwner"
        android:layout_toRightOf="@+id/tvOwner"
        android:layout_toEndOf="@+id/tvOwner"
        android:layout_marginLeft="34dp"
        android:layout_marginStart="34dp"
        android:id="@+id/tvOwnerOutput" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tvPetName"
        android:layout_alignRight="@+id/tvOwnerOutput"
        android:layout_alignEnd="@+id/tvOwnerOutput"
        android:id="@+id/tvPetNameOutput" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView5"
        android:layout_toRightOf="@+id/tvOwner"
        android:layout_toEndOf="@+id/tvOwner"
        android:id="@+id/tvPetTypeOutput" />

    <Button
        android:text="Reset Records"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnInsert"
        android:layout_alignLeft="@+id/tvOwner"
        android:layout_alignStart="@+id/tvOwner"
        android:onClick="ResetRecordSet"
        android:id="@+id/btnResetRecords" />

    <Button
        android:text="Display Current Record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnResetRecords"
        android:layout_alignLeft="@+id/btnResetRecords"
        android:layout_alignStart="@+id/btnResetRecords"
        android:onClick="DisplayCurrentRecord"
        android:id="@+id/btnDisplayCurrentRecord" />
</RelativeLayout>

非常感谢您的帮助

更新

似乎在我的Insert方法中,我实际上并没有插入到PetType列,因为我两次将值插入到PetName


此外,我只得到了3(一半)的结果,因为我在Insert方法的开头和结尾都移动了光标。

您在
DisplayCurrentRecord()
方法中调用了
moveToNext()
两次。这就解释了为什么你只看到了一半的记录。是的,看来我是!有if(cursor.isBeforeFirst()){cursor.moveToNext();}可以吗?据我所知,游标最初在db.query.Yep的第一个结果之前启动,这是正确的。新的
光标将在第一条记录之前启动。只需检查
moveToNext()
返回的内容,就可以稍微简化处理过程。如果它返回
true
,则它可以移动到下一条记录,您可以显示它。如果
为false
,则完成操作,您可以显示您的EOF消息。我在电话上,速度慢且小,@MikeM。你应该把全部写下来answer@PavneetSingh我也在打电话。:-)这就是为什么在我发表评论之前我没有看到你的答案,否则我会对你的答案发表评论来补充这一点。非常欢迎您将这些内容编辑到您的答案中,然后取消删除,这样Ryder就可以接受它,并结束他们的问题。不过谢谢你。干杯
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.devonryder.week9.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:text="Insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="94dp"
        android:id="@+id/btnInsert"
        android:onClick="Insert" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="46dp"
        android:id="@+id/tvInsertOutput" />

    <TextView
        android:text="Owner name "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/btnInsert"
        android:layout_alignEnd="@+id/btnInsert"
        android:layout_marginBottom="85dp"
        android:id="@+id/tvOwner" />

    <TextView
        android:text="Pet Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tvOwner"
        android:layout_alignLeft="@+id/tvOwner"
        android:layout_alignStart="@+id/tvOwner"
        android:layout_marginTop="25dp"
        android:id="@+id/tvPetName" />

    <TextView
        android:text="Pet Type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:id="@+id/textView5"
        android:layout_below="@+id/tvPetName"
        android:layout_alignLeft="@+id/tvPetName"
        android:layout_alignStart="@+id/tvPetName" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tvOwner"
        android:layout_toRightOf="@+id/tvOwner"
        android:layout_toEndOf="@+id/tvOwner"
        android:layout_marginLeft="34dp"
        android:layout_marginStart="34dp"
        android:id="@+id/tvOwnerOutput" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tvPetName"
        android:layout_alignRight="@+id/tvOwnerOutput"
        android:layout_alignEnd="@+id/tvOwnerOutput"
        android:id="@+id/tvPetNameOutput" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView5"
        android:layout_toRightOf="@+id/tvOwner"
        android:layout_toEndOf="@+id/tvOwner"
        android:id="@+id/tvPetTypeOutput" />

    <Button
        android:text="Reset Records"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnInsert"
        android:layout_alignLeft="@+id/tvOwner"
        android:layout_alignStart="@+id/tvOwner"
        android:onClick="ResetRecordSet"
        android:id="@+id/btnResetRecords" />

    <Button
        android:text="Display Current Record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnResetRecords"
        android:layout_alignLeft="@+id/btnResetRecords"
        android:layout_alignStart="@+id/btnResetRecords"
        android:onClick="DisplayCurrentRecord"
        android:id="@+id/btnDisplayCurrentRecord" />
</RelativeLayout>