从数据库中检索的数据显示为“android.database.sqlite”。SQLiteCursor@152ed1d9 "而不是Android 2.2.3

时间:2022-04-14 16:51:45

I've only been coding for a couple of weeks and have no prior experience so please forgive stupid questions and terrible code!

我只做了几个星期的编码,没有以前的经验,所以请原谅愚蠢的问题和糟糕的代码!

The Goal: On the click of a button randomly pull some data from a Database and display it in a TextView.

目标:单击一个按钮,从数据库中随机提取一些数据,并将其显示在TextView中。

The Challenge: It seems to pull the data from the database however what is displayed is the following "android.database.sqlite.SQLiteCursor@XXXXXXXX" where "XXXXXXXX" is made up of numbers and letters (looks like Hexidecimal)

挑战:它似乎从数据库中提取数据,但是显示的是以下“android.databas .sqlite”。SQLiteCursor@XXXXXXXX”,其中“XXXXXXXX”由数字和字母组成(看起来像Hexidecimal)

Here's the code for MainActivity:

这是主活动的代码:

package com.example.mat.databaseclick1;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Button generate = (Button)findViewById(R.id.Generate);
    generate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DBHelper myDatabaseHelper = new DBHelper(MainActivity.this);
            myDatabaseHelper.open();
            String text = String.valueOf(myDatabaseHelper.getRProgram());
            myDatabaseHelper.close();
            TextView result2 = (TextView) findViewById(R.id.result2);
            result2.setText(text);
        }
    });

    DBHelper db = new DBHelper(MainActivity.this);
    //---add a program---
        db.open();
    long id = db.insertProgram("Program One");
    id = db.insertProgram("Program Two");
    id = db.insertProgram("Program Three");
    id = db.insertProgram("Program Four");
    db.close();
    db.open();
    Cursor c = db.getRProgram();
    if (c.moveToFirst())
        DisplayProgram(c);
    else
        Toast.makeText(this, "No program found", Toast.LENGTH_LONG).show();
    db.close();
}
public void DisplayProgram(Cursor c)
{
    TextView result = (TextView) findViewById(R.id.result);
    result.setText("id:" + c.getString(0) + "\n" + "Program:" + c.getString(1));
}
}

And heres the Database Adaptor:

这里是数据库适配器:

package com.example.mat.databaseclick1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.TextView;

import static android.R.attr.data;

public class DBHelper {
static final String KEY_ROWID = "_id";
static final String KEY_NAME = "program";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "MyDB";
static final String DATABASE_TABLE = "programs";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE =
        "create table programs (_id integer primary key autoincrement, "
                + "program text not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBHelper(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS programs");
        onCreate(db);
    }
}


//---opens the database---
public DBHelper open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---
public void close() {
    DBHelper.close();
}

//---insert a program into the database---
public long insertProgram(String program) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, program);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular program---
public boolean deleteProgram(long rowId) {
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

//---retrieves all the programs---
public Cursor getAllPrograms() {
    return db.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_NAME}, null, null, null, null, null);
}

//---retrieves a particular program---
public Cursor getProgram(long rowId) throws SQLException {
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,
                            KEY_NAME},
                    KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//---updates a program---
public boolean updateProgram(long rowId, String program) {
    ContentValues args = new ContentValues();
    args.put(KEY_NAME, program);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}

//---retrieves random program---
public Cursor getRProgram() {
    Cursor mCursor= db.query(DATABASE_TABLE,null,null,null,null,null,"RANDOM()");
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

//----retrieves random program by click-----

    public String[] clicktogetprog() {

        final String DATABASE_TABLE = "programs";

        String rprogresult;
        String selectQuery = "SELECT * FROM " + DATABASE_TABLE;
        SQLiteDatabase db  = this.DBHelper.getWritableDatabase();
        Cursor cursor      = db.query(selectQuery,null,null,null,null,null,"RANDOM()");
        String data      = ("id:" + cursor.getString(0) + "\n" + "Program:" + cursor.getString(1));

        if (cursor.moveToFirst()) {
            do {

                // get the data into array, or class variable
            } while (cursor.moveToNext());
        }
        cursor.close();
        return new String[]{String.valueOf(data)};
    }
}

Any help would be most appreciated!

非常感谢您的帮助!

Thanks in advance :-)

提前谢谢:-)

1 个解决方案

#1


0  

ok just figured it out and its all working. Seems the result I was getting back was something to do with the Cursors position.

好吧,算出来就行了。似乎我得到的结果与游标的位置有关。

To correct it I opened the Database and then moved the cursor ("d" to a random position in the database. I then got it to call "DisplayProgram". In displayprogram I put in the TextView "result2" and then set the text in "result2" by specifying the id with d.getString(0) adn the program with d.getString(1). Here's the working code for MainActivity (I didnt have to change the Database Adapter). Hope it helps someone else out:

为了纠正错误,我打开数据库,然后将光标(“d”)移动到数据库中的随机位置。然后我让它调用"DisplayProgram"在displayprogram中,我输入TextView " t2 result2”,然后通过使用d.getString(0)指定id并使用d.getString(1)调用程序来设置“result2”中的文本。这是MainActivity的工作代码(我不必更改数据库适配器)。希望它能帮助别人:

MainActivity package com.example.mat.databaseclick1;

MainActivity包com.example.mat.databaseclick1;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Button generate = (Button)findViewById(R.id.Generate);
    generate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DBHelper myDatabaseHelper = new DBHelper(MainActivity.this);
            myDatabaseHelper.open();
            Cursor d = myDatabaseHelper.getRProgram();
            if (d.moveToFirst());
            DisplayProgram(d);
            myDatabaseHelper.close();

        }
        public void DisplayProgram (Cursor d){
            TextView result2 = (TextView) findViewById(R.id.result2);
            result2.setText("id:" + d.getString(0) + "\n" + "Program:" + d.getString(1));
        }
    });


    DBHelper db = new DBHelper(MainActivity.this);
    //---add a program---
        db.open();
long id = db.insertProgram("Program One");
id = db.insertProgram("Program Two");
id = db.insertProgram("Program Three");
id = db.insertProgram("Program Four");
    db.close();
    db.open();
    Cursor c = db.getRProgram();
    if (c.moveToFirst())
        DisplayProgram(c);
    else
        Toast.makeText(this, "No program found", Toast.LENGTH_LONG).show();
    db.close();
}



public void DisplayProgram(Cursor c)
{
    TextView result = (TextView) findViewById(R.id.result);
    result.setText("id:" + c.getString(0) + "\n" + "Program:" + c.getString(1));
}

}

}

#1


0  

ok just figured it out and its all working. Seems the result I was getting back was something to do with the Cursors position.

好吧,算出来就行了。似乎我得到的结果与游标的位置有关。

To correct it I opened the Database and then moved the cursor ("d" to a random position in the database. I then got it to call "DisplayProgram". In displayprogram I put in the TextView "result2" and then set the text in "result2" by specifying the id with d.getString(0) adn the program with d.getString(1). Here's the working code for MainActivity (I didnt have to change the Database Adapter). Hope it helps someone else out:

为了纠正错误,我打开数据库,然后将光标(“d”)移动到数据库中的随机位置。然后我让它调用"DisplayProgram"在displayprogram中,我输入TextView " t2 result2”,然后通过使用d.getString(0)指定id并使用d.getString(1)调用程序来设置“result2”中的文本。这是MainActivity的工作代码(我不必更改数据库适配器)。希望它能帮助别人:

MainActivity package com.example.mat.databaseclick1;

MainActivity包com.example.mat.databaseclick1;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Button generate = (Button)findViewById(R.id.Generate);
    generate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DBHelper myDatabaseHelper = new DBHelper(MainActivity.this);
            myDatabaseHelper.open();
            Cursor d = myDatabaseHelper.getRProgram();
            if (d.moveToFirst());
            DisplayProgram(d);
            myDatabaseHelper.close();

        }
        public void DisplayProgram (Cursor d){
            TextView result2 = (TextView) findViewById(R.id.result2);
            result2.setText("id:" + d.getString(0) + "\n" + "Program:" + d.getString(1));
        }
    });


    DBHelper db = new DBHelper(MainActivity.this);
    //---add a program---
        db.open();
long id = db.insertProgram("Program One");
id = db.insertProgram("Program Two");
id = db.insertProgram("Program Three");
id = db.insertProgram("Program Four");
    db.close();
    db.open();
    Cursor c = db.getRProgram();
    if (c.moveToFirst())
        DisplayProgram(c);
    else
        Toast.makeText(this, "No program found", Toast.LENGTH_LONG).show();
    db.close();
}



public void DisplayProgram(Cursor c)
{
    TextView result = (TextView) findViewById(R.id.result);
    result.setText("id:" + c.getString(0) + "\n" + "Program:" + c.getString(1));
}

}

}