Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing a counter from one Activity to another
    text
    copied!<p>I have an class <code>Voice</code>, which extends <code>Activity</code>, and contains a counter. When the user answers correctly, the counter adds one via <code>counter++;</code></p> <pre><code>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; } </code></pre> <p>This score, is bundled and passed on to the next activity "What" within a string "your score is 1". </p> <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == check &amp;&amp; resultCode == RESULT_OK) { ArrayList&lt;String&gt; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); lv.setAdapter( new ArrayAdapter&lt;String&gt;(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(); } } </code></pre> <p>The next <code>Activity</code>, <code>What</code>, gets this <code>Bundle</code> and displays it fine using <code>setText(gotScore)</code></p> <pre><code>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); </code></pre> <p>..this is when things go bad :(</p> <p>On <code>What</code> I have another question tied to a counter as well. When the user answers correctly the counter adds one via <code>counter++;</code> and it does. However, it changes the <code>txview</code> <code>string</code> to "your score is 1". I can't get it to add 1 to the counter result passed from the previous activity within the string, so that the counter on <code>What</code> reads "your score is 2". This gets passes to the next activity in <code>Bundle keeper</code>, which holds the aggregate score.</p> <p>I've read a few tutorials on passing an <code>int</code> verses a <code>string</code>, but some of the code they use like <code>getInt</code> is not recognized. I'm stumped.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload