2012-05-08 58 views
1

我有一個奇怪的問題,我試圖創建一個小工具。該小部件是一個帶有3個答案選項(按鈕)的隨機問題。 我加載從互聯網上的問題,並工作。3個按鈕的Android小部件。和不同的意圖

我混淆了答案,所以答案並不總是在相同的位置。 此外,有可能有多個正確的答案。

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。

什麼問題?或者有更好的方法來做到這一點?

回答

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,意圖意圖,詮釋標誌)」我需要設置2個不同的requestCodes。即使是谷歌開發人員的網頁說,它目前還沒有使用。

  • 上下文:此PendingIntent應該啓動 活動的上下文。
  • requestCode:發件人的私人請求代碼(目前 未使用)。
  • 意圖:要啓動的活動的意圖。
  • 國旗:可能FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,或任何標誌的由 Intent.fillIn所支持()來控制哪些該 可以當實際發送發生所供給的意圖的不特定部分。

因此,我改變這一行:

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

現在它的工作。

相關問題