2012-07-13 150 views
0

我有一個類Voice,它擴展了Activity,幷包含一個計數器。當正確的用戶答案,計數器通過counter++;將計數器從一個活動傳遞到另一個活動

public class Voice extends Activity implements OnClickListener{ 
    ListView lv; 
    static final int check = 111; 
    int counter_score; 
    TextView txView; 
    MediaPlayer ourSong; 
    ImageView display; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.letter_a); 
     initialize(); 
    } 

    private void initialize() { 
     lv = (ListView)findViewById(R.id.lvVoiceReturn); 
     Button b = (Button)findViewById(R.id.imageButtonSelector); 
     txView = (TextView)findViewById(R.id.counter); 
     b.setOnClickListener(this); 
     counter_score=0; 
    } 

這個分數增加一個,捆綁並傳遞到下一個活動「是什麼」的字符串中的「你的分數是1」。

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == check && resultCode == RESULT_OK) { 
     ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
     lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); 

     if(results.contains("hey") || results.contains("a") || results.contains("ay")) { 
      //toast referenced to xml the after 400ms 
      counter_score++; 
      txView.setText("Your Score is" + " " + counter_score); 

      AlertDialog dialogBuilder = new AlertDialog.Builder(this).create(); 
      dialogBuilder.setTitle("AWSOME"); 
      dialogBuilder.setMessage("¡Your current score is" + counter_score); 
      dialogBuilder.setIcon(R.drawable.ic_mark); 
      dialogBuilder.show(); 

      ourSong = MediaPlayer.create(Voice.this, R.raw.rightsound2); 
      ourSong.start(); 
      Thread timer = new Thread() { 
       public void run(){ 
       try { 
        sleep(2500); 
       }catch (InterruptedException e){ 
        e.printStackTrace(); 
       } finally { 
        String score = txView.getText().toString(); 
        Bundle keeper = new Bundle(); 
        keeper.putString("key", score); 
        Intent putScore = new Intent(Voice.this, What.class); 
        putScore.putExtras(keeper); 
        startActivity(putScore); 
       } 
      } 
     }; 

     timer.start(); 
    } 
} 

ActivityWhat,得到這個Bundle,並顯示使用細setText(gotScore)

public class What extends Activity implements OnClickListener { 
    ListView lv; 
    static final int check = 111; 
    private int counter_score; 
    TextView txView; 
    MediaPlayer ourSong; 
    ImageView display; 
    String gotScore; 

    String classes[] = {"What", "Pagina", "What", "example3", "example4", "example5", 
         "example6"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.letter_b); 
     initialize(); 
     Bundle gotKeeper = getIntent().getExtras(); 
     gotScore = gotKeeper.getString("key"); 
     txView.setText(gotScore); 
    } 

    private void initialize() { 
     // TODO Auto-generated method stub 
     lv = (ListView)findViewById(R.id.lvVoiceReturn); 
     Button b = (Button)findViewById(R.id.imageButtonSelector); 
     txView = (TextView)findViewById(R.id.counter); 
     b.setOnClickListener(this); 

..this是,當事情變壞:(

What我還有一個問題綁當用戶正確回答時,計數器通過counter++;增加一個,但是它改變了txviewstring到「你的分數是1」。我無法將它添加到字符串中上一個活動傳遞的計數器結果中,以便What上的計數器顯示「您的分數爲2」。這會傳遞到Bundle keeper中的下一個活動,該活動包含總分。

我讀過一些關於通過int經文string的教程,但他們使用的一些代碼(如getInt)未被識別。我很難過。

+0

從int轉換爲字符串要容易得多,所以要存儲整數。你可以擴展Application類並將你的全局變量存儲在那裏.. – Joel 2012-07-13 22:30:51

回答

0

什麼是捆綁和傳遞到什麼活動不是計數器,但字符串「你的分數是1」。如果你想在下一個活動中增加這個數字,那麼你應該只發送整數值,然後構造你需要的任何字符串。

我已經在傳遞一個int Vs的string..but一些他們使用像getInt不recognized..anywho林難倒代碼讀了幾TUTS ..

我不是太確定我知道你的意思是getInt()無法識別。無論如何,將計數器從一個活動傳遞到另一個活動時,讓自己更容易。如果它是一個int,並且您計劃在接收活動中像int一樣操作,則將其作爲int添加到該包中。例如:

Bundle keeper = new Bundle(); 
    keeper.putInt("key", counter_score); 

而且從同捆檢索:

Bundle gotKeeper = getIntent().getExtras(); 
    int score = gotKeeper.getInt("key"); 
+0

嘿,大家非常感謝你的意見和建議。 – user1446988 2012-07-14 21:00:16

0

,如果你做什麼「全球」類在不同的活動中共享,並用它來保持變量使用「同步」?

例如 - Globals.java:

public class Globals { 

    public int counter_score; 

} 

,接着是參考使用可變 Globals.counter_score

您當然也可以使用其他變量和函數共享類以及 - 例如共同操作。

更新

正如批評家指出,這種方法並不好particularily - 我忘了,代碼簡單地引用,並且不會對自己不「活」,以保持信息的其他活動(感謝您糾正我在那一個,我仍然在學習......)

但是,可以更好地工作的東西,是當你啓動你的第二個活動時通過目的counter_score變量的當前狀態 - 爲例如:

IntentToLaunchTheOtherActivity(counter_score); 

然後可能將變量傳遞迴先前的活動,如果它之後更改...

+0

那麼這不起作用,除非你所有的變量都是靜態的。這個解決方案仍然要求所有其他類都有一個Globals實例..所有這些實例都有自己的變量。 – Joel 2012-07-13 22:25:02

+0

另外,不能保證系統不會殺死你的應用程序,並獲得活動之間的全局變量。這將是一件罕見的事情,但可能會發生。如果你需要確保它永遠不會發生,那麼你會希望將你的計數器存儲在某個文件中,或者意圖來回傳遞它。 – 2012-07-14 01:44:14

0

我明白了。基本上我需要TJ Third建議將keeper.putString("key", counter_score);轉換爲keeper.putInt("key", counter_score);,我還需要將收到的包轉換爲「What」活動中的int。在「What」活動中,我將其更名爲int counter_score;int gotKeeper;(這是String gotKeeper),然後不是調用counter_score = 0;現在傳遞的包是一個int,我稱之爲counter_score = gotKeeper;在initialize()下;所以計數器得分等於先前活動「語音」產生的結果。

現在當用戶正確回答時,counter ++;將其添加到現有的counter_score並將其捆綁併發送到下一個活動,然後沖洗重複。

static final int check = 111; 
    int counter_score; 
    TextView txView; 
    MediaPlayer ourSong; 
    ImageView display; 
    int gotKeeper; 


    String classes[] = {"What", "Pagina", "What", "example3", "example4", "example5", 
    "example6"}; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.letter_b); 
     initialize(); 
     Bundle gotKeeper = getIntent().getExtras(); 
     gotKeeper = gotScore.getInt("key"); 
     counter_score = gotKeeper; 

再次thinx給大家的建議和insight.Huge幫助新手。

+0

如果您有進展的更新,請編輯您的問題並在那裏寫入更新的信息。如果某個特定的答案(例如我的答案)有助於解決您的問題,請接受答案。 [請參閱常見問題了解更多關於StackOverflow的指導方針和禮儀的信息](http://stackoverflow.com/faq) – 2012-07-14 22:58:15

相關問題