我有一些按钮,并且设置了一些文本,我需要这些文本在活动开始时可见。之后,我从 sqlite 数据库为我的所有按钮设置了一些文本,但我不希望在用户单击按钮之前显示该文本。
最简单的解决方案是使用 button.setText(c.getString(int)); 并在单击按钮时从 db 设置该文本,但我的 c 光标不在活动范围内,因此我无法在按钮的 onClick 方法中使用它。我试图将所有内容都放在我的 nextQuestion() 方法中的活动范围内,但这给了我错误。
所以我想我应该隐藏数据库中的所有文本集,直到按下按钮,取消隐藏它。怎么做?或者,如果第一个想法是可能的,那就更好了。
这是我的游戏课:
public class Game extends Activity implements View.OnClickListener{
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
MediaPlayer buttonClicks;
public boolean music;
LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();
private String generateWhereClause(){
StringBuilder result = new StringBuilder();
for (Long l : mAnsweredQuestions){
result.append(" AND _ID <> " + l);
}
return result.toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
addListenerOnButton();
nextQuestion();
}
private void addListenerOnButton() {
buttonClicks = MediaPlayer.create(this, R.raw.button);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
b4 = (Button) findViewById(R.id.button4);
b5 = (Button) findViewById(R.id.button5);
b6 = (Button) findViewById(R.id.button6);
b7 = (Button) findViewById(R.id.button7);
b8 = (Button) findViewById(R.id.button8);
b9 = (Button) findViewById(R.id.button9);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
}
private void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
b1.setText(c.getString(2));
b2.setText(c.getString(3));
b3.setText(c.getString(4));
b4.setText(c.getString(5));
b5.setText(c.getString(6));
b6.setText(c.getString(7));
b7.setText(c.getString(8));
b8.setText(c.getString(9));
b9.setText(c.getString(10));
}
finally{
mDbHelper.close();
}
}
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
if(music == true){
buttonClicks.start();
}
b1.setText(c.getString(2));// this is a good option, but can't use cursor
b2.setEnabled(true);
break;
case R.id.button2:
if(music == true){
buttonClicks.start();
}
b3.setEnabled(true);
break;
case R.id.button3:
if(music == true){
buttonClicks.start();
}
break;
case R.id.button4:
if(music == true){
buttonClicks.start();
}
}
}
}