任何人都可以帮助解决我的小问题。每当我试图设置来自数据库的按钮文本时,它就不会显示,但显示了文本视图的txt。
活动
package com.iamrock.louie.saver.EasyMode;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.List;
import android.widget.TextView;
import com.iamrock.louie.saver.Choices.Correct;
import com.iamrock.louie.saver.Choices.Lose;
import com.iamrock.louie.saver.Choices.Wrong;
import com.iamrock.louie.saver.R;
public class EasyGame extends Activity {
ImageView eview;
int x;
List<Question> quesList;
public int qid;
TextView txtQuestion;
Button choice, choices;
Question currentQ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_game);
qid = getIntent().getExtras().getInt("asd");
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.questtxt1);
choice=(Button) findViewById(R.id.choice1);
choices=(Button) findViewById(R.id.choice2);
condition();
if(x<5){
txtQuestion.setText(currentQ.getQUESTION());
choice.setText(currentQ.getOPTA());
choices.setText(currentQ.getOPTB());
//qid++;
}
choice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button answer= (Button)findViewById(R.id.choice1);
if(currentQ.getANSWER().equals(answer.getText()))
{
Intent intent = new Intent(EasyGame.this, Correct.class);
intent.putExtra("asd", qid);
startActivity(intent);
}
else
{//wrong answer statement
Intent intent = new Intent(EasyGame.this, Wrong.class);
intent.putExtra("asd", qid);
intent.putExtra("zxc", x+=1);
startActivity(intent);
}
}
});
choices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button answer= (Button)findViewById(R.id.choice2);
if(currentQ.getANSWER().equals(answer.getText()))
{
Intent intent = new Intent(EasyGame.this, Correct.class);
intent.putExtra("asd", qid);
startActivity(intent);
}
else
{
Intent intent = new Intent(EasyGame.this, Wrong.class);
intent.putExtra("asd", qid);
intent.putExtra("zxc", x+=1);
startActivity(intent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.easy_game, menu);
return true;
}
private void condition()
{
if(x==0)
{
eview = (ImageView)findViewById(R.id.puso1);
eview.setVisibility(View.VISIBLE);
}
else
if (x==1)
{
eview = (ImageView)findViewById(R.id.puso1);
eview.setVisibility(View.INVISIBLE);
}
else
if (x==2)
{
eview = (ImageView)findViewById(R.id.puso2);
eview.setVisibility(View.INVISIBLE);
}
else
if (x==3)
{
eview = (ImageView)findViewById(R.id.puso3);
eview.setVisibility(View.INVISIBLE);
}
else
if (x==4)
{
eview = (ImageView)findViewById(R.id.puso4);
eview.setVisibility(View.INVISIBLE);
}
else
if (x==5)
{
Intent intent = new Intent(EasyGame.this,Lose.class);
startActivity(intent);
eview = (ImageView)findViewById(R.id.puso5);
eview.setVisibility(View.INVISIBLE);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.iamrock.louie.homepage.HardGame"
android:background="@drawable/quest_bg"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choice1"
android:id="@+id/choice1"
android:layout_marginBottom="110dp"
android:background="@android:color/transparent"
android:textColor="#f1fa02"
android:layout_above="@+id/choice2"
android:layout_toRightOf="@+id/questtxt1"
android:layout_toEndOf="@+id/questtxt1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choice2"
android:id="@+id/choice2"
android:layout_marginBottom="60dp"
android:background="@android:color/transparent"
android:textColor="#f1fa02"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/choice1"
android:layout_alignStart="@+id/choice1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="question"
android:id="@+id/questtxt1"
android:textSize="24dp"
android:layout_toEndOf="@+id/puso4"
android:textColor="#ff000000"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="200dp"
android:layout_toRightOf="@+id/puso4"
android:layout_alignBottom="@+id/puso2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/puso5"
android:src="@drawable/life"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/puso4"
android:src="@drawable/life"
android:layout_toRightOf="@+id/puso5"
android:layout_marginRight="1px"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/puso3"
android:src="@drawable/life"
android:visibility="visible"
android:layout_below="@+id/puso5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/puso2"
android:src="@drawable/life"
android:layout_below="@+id/puso5"
android:layout_toRightOf="@+id/puso1"
android:layout_toEndOf="@+id/puso1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/puso1"
android:src="@drawable/life"
android:layout_below="@+id/puso2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<com.iamrock.louie.saver.Animation.GifViewQ
android:id="@+id/gifviewq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/choice1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/choice1"
android:layout_toStartOf="@+id/choice1"
android:layout_marginRight="20dp"
/>
这是我认为是问题的数据库和助手
package com.iamrock.louie.saver.EasyMode;
public class Question {
private int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String ANSWER;
public Question()
{
ID=0;
QUESTION="";
OPTA="";
OPTB="";
ANSWER="";
}
public Question(String qUESTION, String oPTA, String oPTB,
String aNSWER) {
QUESTION = qUESTION;
OPTA = oPTA;
OPTB = oPTB;
ANSWER = aNSWER;
}
public int getID()
{
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getOPTA() {
return OPTA;
}
public String getOPTB() {
return OPTB;
}
public String getANSWER() {
return ANSWER;
}
public void setID(int id)
{
ID=id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setOPTA(String oPTA) {
OPTA = oPTA;
}
public void setOPTB(String oPTB) {
OPTB = oPTB;
}
public void setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}
辅助
package com.iamrock.louie.saver.EasyMode;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "saver";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Question q1=new Question("What are you going to do when earthquake" +
" occurs and you're top in the building?",
" A. find something e.g.\n" +
" table that you can hide",
" B. stay on the top\n" +
" of the buiding",
" A. find something e.g.\n" +//answer
" table that you can hide");
this.addQuestion(q1);
Question q2=new Question("Sometimes there are small tremors that occur preceding an earthquake. \n" +
"These are called “ ”?\n", "A. Foreshocks", "B. Aftershocks",
"A. Foreshocks");//answer
this.addQuestion(q2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addQuestion(Question quest) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
答案 0 :(得分:0)
在此代码之前,您尚未指定x
的值if(x<5){
txtQuestion.setText(currentQ.getQUESTION());
choice.setText(currentQ.getOPTA());
choices.setText(currentQ.getOPTB());
//qid++;
}
在此条件之前为x赋值。这可以解决你的问题。