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

时间:2012-12-29 13:21:30

标签: android sql database

我正在制作一个使用数据库中的数据的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" />

1 个答案:

答案 0 :(得分:1)

代码经过测试且工作正常。祝你的项目好运。 http://uploaded.net/file/9dqsjc9j 提示:如果您的java文件中的eclipse 缺少导入,请按 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";
}