1

作成しようとしているウィジェットに奇妙な問題があります。ウィジェットは、3 つの回答オプション (ボタン) を持つランダムな質問です。インターネットから質問をロードすると、うまくいきます。

答えをごちゃまぜにしているので、答えがいつも同じ場所にあるとは限りません。また、複数の正解が存在する可能性もあります。

public RemoteViews setAnswers(JSONObject questionObj, RemoteViews views) throws JSONException {
    // Get the Answers
    String answerA = questionObj.getString("Ans1");
    String answerB = questionObj.getString("Ans2");
    String answerC = questionObj.getString("Ans3");

    // Get the Answers that is correct (1 = Correct, 0 = Wrong)
    int correctA = Integer.parseInt(questionObj.getString("correct1"));
    int correctB = Integer.parseInt(questionObj.getString("correct2"));
    int correctC = Integer.parseInt(questionObj.getString("correct3"));

    // Get help text for correct or wrong answer (HTML)
    String HTTPcor = questionObj.getString("HTTPcor")+" - Correct";
    String HTTPwro = questionObj.getString("HTTPwro")+" - Wrong";

    // Create the Intent for the answers that is correct
    Intent correctIntent = new Intent(maincontext, AdMain.class);
    correctIntent.putExtra("appWidID", appWidId);
    correctIntent.putExtra("correct", 1); // Answer is correct.
    correctIntent.putExtra("HTTP", HTTPcor); // Help text.
    PendingIntent correctIntentPending = PendingIntent.getActivity(maincontext, 0, correctIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Create the Intent for the answers that is wrong
    Intent wrongIntent = new Intent(maincontext, AdMain.class);
    wrongIntent.putExtra("appWidID", appWidId);
    wrongIntent.putExtra("correct", 0); // Answer is wrong.
    wrongIntent.putExtra("HTTP", HTTPwro); // Help text.
    PendingIntent wrongIntentPending = PendingIntent.getActivity(maincontext, 0, wrongIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Create a random number between 1 and 6 (6 different ways to mix up 3 buttons)
    Random generator = new Random();
    int mix = generator.nextInt(6) + 1;
    Log.v("JapanWidget", "AnswerMix - "+mix+" - appWidId = "+appWidId);

    // DEBUG Set mix to 1 Until i get the Intent fixed.
    mix = 1;

    // Mix the buttons.
    switch(mix) {
      case 1:
          views.setTextViewText(R.id.Answer1, answerA); // Button 1 answer A
          // If correctA = 1 THEN set correctIntentPending ELSE set wrongIntentPending
          if (correctA == 1) { views.setOnClickPendingIntent(R.id.Answer1, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer1, wrongIntentPending); }
          views.setTextViewText(R.id.Answer2, answerB); // Button 2 answer B
          // If correctB = 1 THEN set correctIntentPending ELSE set wrongIntentPending
          if (correctB == 1) { views.setOnClickPendingIntent(R.id.Answer2, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer2, wrongIntentPending); }
          views.setTextViewText(R.id.Answer3, answerC); // Button 3 answer C
          // If correctB = 1 THEN set correctIntentPending ELSE set wrongIntentPending
          if (correctC == 1) { views.setOnClickPendingIntent(R.id.Answer3, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer3, wrongIntentPending); }
          break;
      default:  
          views.setTextViewText(R.id.Answer1, answerA);
          if (correctA == 1) { views.setOnClickPendingIntent(R.id.Answer1, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer1, wrongIntentPending); }
          views.setTextViewText(R.id.Answer2, answerB);
          if (correctB == 1) { views.setOnClickPendingIntent(R.id.Answer2, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer2, wrongIntentPending); }
          views.setTextViewText(R.id.Answer3, answerC);
          if (correctC == 1) { views.setOnClickPendingIntent(R.id.Answer3, correctIntentPending); } else { views.setOnClickPendingIntent(R.id.Answer3, wrongIntentPending); }
          break;
    }

    return views;
}

問題は、新しいアクティビティから GetExtra を実行すると、常に wrongIntentPending に設定されているエクストラになることです。ここで何かがうまくいかない。しかし、「if (correctA == 1) { } else { }」は正しく機能することがわかります。「if (correctA == 1) { } else { }」に設定しても、wrongIntentPending から Extra を取得します。

何がうまくいかないのですか?または、これを行うより良い方法はありますか?

4

1 に答える 1

3

このページを読んだ後、問題を見つけました:http://www.bogdanirimia.ro/android-widget-click-event-multiple-instances/269

作業コード:

// Create the Intent for the answers that is correct
Intent correctIntent = new Intent(maincontext, AdMain.class);
correctIntent.putExtra("appWidID", appWidId);
correctIntent.putExtra("correct", 1); // Answer is correct.
correctIntent.putExtra("HTTP", HTTPcor); // Help text.
PendingIntent correctIntentPending = PendingIntent.getActivity(maincontext, 0, correctIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// Create the Intent for the answers that is wrong
Intent wrongIntent = new Intent(maincontext, AdMain.class);
wrongIntent.putExtra("appWidID", appWidId);
wrongIntent.putExtra("correct", 0); // Answer is wrong.
wrongIntent.putExtra("HTTP", HTTPwro); // Help text.
PendingIntent wrongIntentPending = PendingIntent.getActivity(maincontext, 1, wrongIntent, PendingIntent.FLAG_UPDATE_CURRENT);

「PendingIntent getActivity (コンテキスト コンテキスト、int requestCode、インテント インテント、int フラグ)」では、2 つの異なる requestCode を設定する必要がありました。Google開発者ページでも、現在使用されていないと言っています。

  • context: この PendingIntent がアクティビティを開始するコンテキスト。
  • requestCode: 送信者のプライベート リクエスト コード (現在は使用されていません)。
  • インテント: 起動されるアクティビティのインテント。
  • flags: FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT、FLAG_UPDATE_CURRENT、または Intent.fillIn() でサポートされているフラグのいずれかで、実際の送信時に提供できるインテントの未指定部分を制御します。

だから私はこの行を変更しました:

PendingIntent wrongIntentPending = PendingIntent.getActivity(maincontext, 1 , wrongIntent, PendingIntent.FLAG_UPDATE_CURRENT);

そして今、それは機能します。

于 2012-05-10T06:57:37.307 に答える