我有 36 个 imageButtons,每一个都调用同一个弹出窗口,在其中我从 sqlite 数据库中设置随机问题。当用户通过单击 4 个可能的解决方案之一来回答问题时,它会关闭并返回到我的 36 个按钮。现在我有两个问题:
我使用此代码来避免重复问题。在活动范围内:
LinkedList mAnsweredQuestions = new LinkedList();
private String generateWhereClause(){ StringBuilder result = new StringBuilder(); for (Long l : mAnsweredQuestions){ result.append(" AND _ID <> " + l); } return result.toString(); }
在我的问题方法中:
mAnsweredQuestions.add(c.getLong(0));
但是,当弹出问题关闭后我的活动恢复时,这不起作用。就像它从头开始加载我的活动一样。我在我的其他游戏中使用它,它在“正常”活动中运行良好,但由于某种原因在弹出后它没有。
2.我需要为每个 imageButton 按下来启动这个弹出活动,并在每个正确答案之后保存不同数量的点。我在其他游戏中使用 startActivityForResult 来启动活动并保存结果,但在这种情况下,我使用每个按钮开始不同的活动。现在我必须开始相同的活动,并保存不同的结果。我怎样才能做到这一点?这是我的弹出问题类:
public class Pitanja_Cigle extends Activity{
public static String tacanOdg;
int counter = 0;
Button b1, b2, b3, b4;
TextView question;
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();
}
Runnable mLaunchTaskFinish = new Runnable() {
public void run() {
finish();
}
};
private class Answer {
public Answer(String opt, boolean correct) {
option = opt;
isCorrect = correct;
}
String option;
boolean isCorrect;
}
Handler mHandler = new Handler();
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
Answer ans = (Answer) v.getTag();
if (ans.isCorrect) {
Intent i = new Intent("rs.androidaplikacije.toplo_hladno.TACANODGOVOR");
startActivity(i);
mHandler.postDelayed(mLaunchTaskFinish,1200);
}
else{
Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
i.putExtra("tacanOdgovor", tacanOdg);
startActivity(i);
mHandler.postDelayed(mLaunchTaskFinish,2200);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.pitanja_cigle);
InicirajVariable();
nextQuestion();
}
private void nextQuestion() {
counter++;
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{ //Pokusava da otvori db
mDbHelper.open(); //baza otvorena
Cursor c = mDbHelper.getPitanjaCigle(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
List<Answer> labels = new ArrayList<Answer>();
labels.add(new Answer(c.getString(2), true));
labels.add(new Answer(c.getString(3), false));
labels.add(new Answer(c.getString(4), false));
labels.add(new Answer(c.getString(5), false));
tacanOdg = c.getString(2);
Collections.shuffle(labels);
question.setText(c.getString(1));
b1.setText(labels.get(0).option);
b1.setTag(labels.get(0));
b1.setOnClickListener(clickListener);
b2.setText(labels.get(1).option);
b2.setTag(labels.get(1));
b2.setOnClickListener(clickListener);
b3.setText(labels.get(2).option);
b3.setTag(labels.get(2));
b3.setOnClickListener(clickListener);
b4.setText(labels.get(3).option);
b4.setTag(labels.get(3));
b4.setOnClickListener(clickListener);
}
finally{ // kada zavrsi sa koriscenjem baze podataka, zatvara db
mDbHelper.close();
}
}
private void InicirajVariable() {
Typeface naslov = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
question = (TextView) findViewById(R.id.tvPitanjeCigle);
b1 = (Button) findViewById(R.id.bOdgCigle1);
b2 = (Button) findViewById(R.id.bOdgCigle2);
b3 = (Button) findViewById(R.id.bOdgCigle3);
b4 = (Button) findViewById(R.id.bOdgCigle4);
b1.setTypeface(dugmad);
b2.setTypeface(dugmad);
b3.setTypeface(dugmad);
b4.setTypeface(dugmad);
question.setTypeface(naslov);
}
}