0

我有一个游戏活动并使用 sqlite 数据库填充一些按钮,随机不重复,当游戏完成时,我会弹出一个带有 OK 按钮的警报对话框和一些信息。现在,当我按下 OK 时,我需要关闭该弹出窗口,并在该游戏活动中将一些其他数据重新加载到我的按钮,而无需重复。这我需要发生两次。我在我的其他游戏中执行此操作,但该弹出窗口没有“确定”按钮,它弹出 3 秒并使用处理程序关闭,并且我的活动重新加载新数据而不重复,这就像一个魅力。在这个游戏中,只有确定按钮是有区别的,它不起作用。我需要那个确定按钮。我有两个问题。

  1. 我成功地重新加载了我的活动,但重复了,这告诉我我的活动是从头开始加载的。
  2. 当活动第二次加载时,当我按下后退按钮时,我可以看到以前的游戏活动,这再次告诉我我有一个活动加载了两次。我每次只需要一次新数据。此外,我的计数器不起作用,因为在第二次加载后,我得到了第 3 次、第 4 次等等。

这是我的游戏和弹出活动:

public class ToploHladno extends Activity implements View.OnClickListener{

    Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, bIzlaz, bDalje;
    String mButtonText1, mButtonText2, mButtonText3, mButtonText4, mButtonText5, mButtonText6,
    mButtonText7, mButtonText8, mButtonText9, mButtonText10, opis, odgovorNormalized;
    MediaPlayer buttonClicks, buttonFinal, buttonBack;
    public boolean music;
    final Context context = this;
    Editable ukucanRezultat;
    String tacanRezultat, ukucanRezultatVelikaSlova;
    int brojPoena;
    int counter = 0;


    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() {


        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        bIzlaz = (Button) findViewById(R.id.bIzlaz);
        bDalje = (Button) findViewById(R.id.bDalje);

    }

    public void nextQuestion() {

        counter++;

        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();

        try{ 

            mDbHelper.open();

            Cursor c = mDbHelper.getTestData(generateWhereClause());

            mAnsweredQuestions.add(c.getLong(0));

            mButtonText1 = c.getString(2);
            mButtonText2 = c.getString(3);
            mButtonText3 = c.getString(4);
            mButtonText4 = c.getString(5);

            b1.setOnClickListener(this);
            b2.setOnClickListener(this);
            b3.setOnClickListener(this);
            b4.setOnClickListener(this);


            if(counter < 3){

                b1.setText("30 poena");
                b2.setText("25 poena");
                b3.setText("22 poena");
                b4.setText("20 poena");

            }else{
                finish();
            }

        }

            finally{ 
                mDbHelper.close();
            }

    }

弹出窗口:

public class Popup_opis extends Activity implements OnClickListener{

    TextView tvOpis;
    int brojPoenaPrimljeno;
    Button OK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.popup_opis);

        initVariables();

    }

    private void initVariables() {



        OK = (Button) findViewById(R.id.bOK);
        tvOpis = (TextView) findViewById(R.id.tvOpis);


    OK.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            finish();
            Intent intent = new Intent(Popup_opis.this, ToploHladno.class);
            //intent.putExtra("myMethod", "nextQuestion"); //I tried also this
            startActivity(intent);

        }
    });
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}
4

1 回答 1

1

如果我理解正确,您ToploHladno Activity将从Popup_opis Activity使用Intent. 如果ToploHladno Activity还没有完成,我认为没有完成,那么就不需要Intent. 您可以invalidate()在调用它们之后调用Views您想要重新填充的对象,或者您需要对它们进行任何更改onResume()setText()Views

@Override
public void onResume()   
{
    // change your Buttons however you need in here then say you have btn1
    btn1.invalidate();
}

您的第一个Activity尚未完成,因此您不需要甚至可能不想以Intent. 您将在关闭后返回它,Dialog Activity并且onResume()会被调用,以便您可以在那里做您需要的事情。这将使您无法Layout通过不创建 . 的新实例来重新绘制整体Activity。让我知道这是否适合您的需要

无效

startActivityForResult()例子:

Intent intent = new Intent(ToploHladno .this, Popup_opis .class);
intent.putExtra("counter", counter);
startActivityForResult( intent, 0 );  //second param is a request code. You will need this later

然后在弹出活动中

public class Popup_opis extends Activity implements OnClickListener{

TextView tvOpis;
int brojPoenaPrimljeno;
Button OK;
int counter;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.popup_opis);

        //Get intent
        Intent recIntent = getIntent();
         counter = recIntent.getIntExtra("counter");   //get counter variable from previous activity

OK.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
       counter++;

        Intent intent = new Intent(Popup_opis.this, ToploHladno.class);
        intent.putExtra("counter", counter); //SEND BACK COUNTER
        setResult(RESULT_OK, intent);  // send result
finish();

然后在你之前的活动中

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
// would use REQUEST CODE sent earlier if more than one activity sends back intents here
    if (resultCode == RESULT_OK)
    {
        counter = data.getInteExtra("counter", 0);  // get updated counter variable
    }
}

更多关于 Docs 中的 startActivityForResult

于 2013-04-06T15:46:28.743 回答