我必须为大学项目制作一个Hangman应用程序,目前我已经创建了一个SQLite数据库,并在那里放了几个单词。我正在尝试运行一个查询来选择一个随机单词并在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);
}
}
答案 0 :(得分:0)
创建自己的Hungman应用程序。 创建基础DBHelper。添加一些单词进行测试。设置刽子手逻辑。
最好动态创建UI元素。创建布局作为新元素的框,但以编程方式将其放在那里。
private LinearLayout lettersLayout;
private final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; ***
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hangman);
lettersLayout = (LinearLayout) findViewById(R.id.lettersLay); **
for (String alphabetLetter : alphabet.split("(?!^)")) {
TextView alphabetLetterTxt = new TextView(this);
alphabetLetterTxt.setText(alphabetLetter);
lettersLayout.addView(alphabetLetter);
}
以下是使用SQLitte数据库的简单示例:
public class DBHelper extends SQLiteOpenHelper {
private static DBHelper instance;
private static final String DATABASE_NAME = "HangmanBase";
private static final int DATABASE_VERSION = 5;
public static interface WORD extends BaseColumns {
String TABLE_NAME = "wordTable";
String WORD = "word";
String DESCRIPTION = "wordDescription";
}
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS ";
private static final String UNIQUE = "UNIQUE ON CONFLICT ABORT";
private static final String CREATE_TABLE_WORD = CREATE_TABLE + WORD.TABLE_NAME + " (" + WORD._ID
+ " INTEGER PRIMARY KEY, " + WORD.WORD + " TEXT " + UNIQUE + ", " + WORD.DESCRIPTION + " TEXT);";
private DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static DBHelper getInstance(Context context) {
if (instance == null) {
instance = new DBHelper(context);
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_WORD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE + WORD.TABLE_NAME);
onCreate(db);
}
public long addWord(String newWord, String newWordDescription) {
ContentValues values = new ContentValues();
values.put(WORD.WORD.toUpperCase(Locale.UK), newWord);
values.put(WORD.DESCRIPTION, newWordDescription);
return getWritableDatabase().insert(WORD.TABLE_NAME, null, values);
}
public HangmanWord getRandomWord() {
Cursor allWordsList = getReadableDatabase().rawQuery("SELECT * FROM " + WORD.TABLE_NAME, null);
if (allWordsList != null && allWordsList.getCount() > 0) {
Random random = new Random();
allWordsList.moveToPosition(random.nextInt(allWordsList.getCount()));
return new HangmanWord(allWordsList.getString(allWordsList.getColumnIndex(WORD.WORD)),
allWordsList.getString(allWordsList.getColumnIndex(WORD.DESCRIPTION)));
}
return null;
}
}