1
我必須爲大學項目製作一個Hangman應用程序,此刻我創建了一個SQLite數據庫,並在其中放了幾個字。我試圖運行一個查詢來選擇一個隨機的單詞並將其顯示在textview中,但是我無法獲得顯示的結果。如何在textview中顯示數據庫查詢?
這是我迄今爲止
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
class Hangman extends Activity {
public TextView wordText;
public Button button02A;
private Button button01B;
private Button button03C;
private Button button04D;
private Button button05E;
private Button button06F;
private Button button07G;
private Button button1H;
private Button button09I;
private Button button10J;
private Button button11K;
private Button button12L;
private Button button13M;
private Button button14N;
private Button button15O;
private Button button16P;
private Button button18Q;
private Button button19R;
private Button button20S;
private Button button21T;
private Button button17U;
private Button button22V;
private Button button23W;
private Button button08X;
private Button button25Y;
private Button button26Z;
private Button button24Voice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hangman);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_hangman, menu);
return true;
}
public void assignButtons() {
wordText = (TextView) findViewById(R.id.wordText);
button02A = (Button) findViewById(R.id.Button02);
button01B = (Button) findViewById(R.id.Button01);
button03C = (Button) findViewById(R.id.Button03);
button04D = (Button) findViewById(R.id.Button04);
button05E = (Button) findViewById(R.id.Button05);
button06F = (Button) findViewById(R.id.Button06);
button07G = (Button) findViewById(R.id.Button07);
button1H = (Button) findViewById(R.id.button1);
button09I = (Button) findViewById(R.id.Button09);
button10J = (Button) findViewById(R.id.Button10);
button11K = (Button) findViewById(R.id.Button11);
button12L = (Button) findViewById(R.id.Button12);
button13M = (Button) findViewById(R.id.Button13);
button14N = (Button) findViewById(R.id.Button14);
button15O = (Button) findViewById(R.id.Button15);
button16P = (Button) findViewById(R.id.Button16);
button18Q = (Button) findViewById(R.id.Button18);
button19R = (Button) findViewById(R.id.Button19);
button20S = (Button) findViewById(R.id.Button20);
button21T = (Button) findViewById(R.id.Button21);
button17U = (Button) findViewById(R.id.Button17);
button22V = (Button) findViewById(R.id.Button22);
button23W = (Button) findViewById(R.id.Button23);
button08X = (Button) findViewById(R.id.Button08);
button25Y = (Button) findViewById(R.id.Button25);
button26Z = (Button) findViewById(R.id.Button26);
button24Voice = (Button) findViewById(R.id.Button24);
}
}
class NewDatabase extends SQLiteOpenHelper {
//Create Database
private static final String DATABASE_NAME = "myDatabase.db";
private static final String DATABASE_TABLE ="Puzzles";
public static final String COLUMN_PUZZLES = "Puzzlescol";
public static final String COLUMN_ID = "_id";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
+ DATABASE_TABLE + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_PUZZLES
+ " text not null);";
public NewDatabase(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
//Overriding onCreate and onUpgrade to create database
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(NewDatabase.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public void insertRows(){
ContentValues values = new ContentValues();
values.put(COLUMN_ID, "1");
values.put(COLUMN_PUZZLES, "MERMAID");
values.put(COLUMN_ID, "2");
values.put(COLUMN_PUZZLES, "SUITCASE");
values.put(COLUMN_ID, "3");
values.put(COLUMN_PUZZLES, "OSTRICH");
}
public String randWord() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.query("DATABASE_TABLE Order BY RANDOM() LIMIT 1",
new String[] { "*" }, null, null, null, null, null);
return cur.getString(cur.getColumnIndex(COLUMN_PUZZLES));
}
public void showWord(Cursor cur) {
wordText.setText(cur);
}
}
。首先設置一個點擊監聽器到任何按鈕,創建一個NewDatabase類的實例來插入或從數據庫中選擇值。你也可以看到[this](http://www.androidhive.info/2011/11/android-sqlite-database -tutorial /)教程 –
你有什麼樣的麻煩? –
謝謝我會看看這個教程。 – Pedroson