2012-07-13 149 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

相关问题