小编典典

如何从数据库中选择随机数据并在textview中显示?

sql

我正在制作一个使用数据库中数据的android报价应用程序。当我单击下一步按钮时,我想在列表视图中显示随机报价。顺便说一句,我每次打开应用程序时如何保存选择的内容?请帮忙!这是我的数据库类:

 public class SQLiteAdapter {
    public static final String MYDATABASE_NAME = "MY_DATABASE";
    public static final String MYDATABASE_TABLE = "MY_TABLE";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT = "Content";

//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
    "create table " + MYDATABASE_TABLE + " ("
    + KEY_ID + " integer primary key autoincrement, "
    + KEY_CONTENT + " text not null);";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public SQLiteAdapter(Context c){
    context = c;
}

public SQLiteAdapter openToRead() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,  MYDATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    return this;    
}

public SQLiteAdapter openToWrite() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    return this;    
}

public void close(){
    sqLiteHelper.close();
}

public long insert(String content){

    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_CONTENT, content);
    return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}

public int deleteAll(){
    return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}

public Cursor queueAll(){
    String[] columns = new String[]{KEY_ID, KEY_CONTENT};
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
            null, null, null, null, null);

    return cursor;
}

public class SQLiteHelper extends SQLiteOpenHelper {

    public SQLiteHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(SCRIPT_CREATE_DATABASE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

}

这是我要显示报价的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"

  />

<Button
    android:id="@+id/next"
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/textView1"
    android:paddingBottom="5dip"
    android:paddingTop="5dip"
    android:text="Next"
    android:textColor="@android:color/black" />

阅读 251

收藏
2021-05-16

共1个答案

小编典典

该代码已经 过测试,可以正常工作
。祝您的项目好运。http://uploaded.net/file/9dqsjc9j
提示 :如果eclipse 缺少 Java文件中的 导入 ,请按 CTRL + SHIFT + O

AndroidSqlite.java

@Override
public void onCreate(Bundle savedInstanceState) {
    // ....
    mySQLiteAdapter = new SQLiteAdapter(this);
    mySQLiteAdapter.openToWrite();
    mySQLiteAdapter.deleteAll();

    mySQLiteAdapter.insert("A for Apply");
    // ....
    mySQLiteAdapter.insert("Z for Zoo");

    final TextView tv = (TextView)findViewById(R.id.textView1);
    Button btn = (Button)findViewById(R.id.next);
    btn.setOnClickListener(new OnClickListener(){
        public void onClick(View v)
        {
        tv.setText(mySQLiteAdapter.getRandomQuote());
        }

    });
}

@Override
public void onDestroy()
{
    super.onDestroy();
    mySQLiteAdapter.close();
}

将其放在您的SQLiteAdapter.java中

public String getRandomQuote()
{
Cursor c = sqLiteDatabase.query(MYDATABASE_TABLE + " ORDER BY RANDOM() LIMIT 1",
        new String[] { KEY_CONTENT }, null, null, null, null, null);

if(c.moveToFirst())
  return c.getString(c.getColumnIndex(KEY_CONTENT));
else 
  return "nothing";
}
2021-05-16