Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one make this code cleaner?
    text
    copied!<p>I'm working with 2 activities. One activity has a textView and a button. The textView shows the hobbies, which will be set if the user clicks the button. When the user clicks the button, he/she will go to the second activity which has 4 checkboxes corresponding to hobbies. When the user clicks Done from a menu, he/she will go back to the first activity, afterwhich, the checked hobbies will be shown in the textview, separated by commas (","). My problem is that, when the user sets the hobbies using the button again, but already has hobbies showing in the textView, the checkboxes in the second Activity should be initialized according to what is shown in the textView. I have tried arrays, even hardcoding, but I am stuck in a logical rut. Here is my code. Assume all views have been initialized:</p> <p>1st Screen (important bit):</p> <pre><code>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case SET_BIRTHDAY: TextView textBirthday = (TextView) findViewById(R.id.tvBirthdayStat); birthdayString = data.getStringExtra(Lab2_082588birthday.MONTH) + "/" + data.getStringExtra(Lab2_082588birthday.DAY) + "/" + data.getStringExtra(Lab2_082588birthday.YEAR); textBirthday.setText(birthdayString); break; case SET_HOBBIES: TextView textHobbies = (TextView) findViewById(R.id.tvHobbiesStat); anime = data.getStringExtra(Lab2_082588hobbies.ANIME); games = data.getStringExtra(Lab2_082588hobbies.GAMES); movies = data.getStringExtra(Lab2_082588hobbies.MOVIES); books = data.getStringExtra(Lab2_082588hobbies.BOOKS); checkNull(); textHobbies.setText(hobbiesString); break; } } } private void checkNull() { // TODO Auto-generated method stub if (games == null) { hobbiesString = books + " , " + movies + " , " + anime; } if (anime == null) { hobbiesString = games + " , " + books + " , " + movies; } if (movies == null) { hobbiesString = games + " , " + books + " , " + anime; } if (books == null) { hobbiesString = games + " , " + movies + " , " + anime; } if ((games == null) &amp;&amp; (anime == null)) { hobbiesString = books + " , " + movies; } if ((games == null) &amp;&amp; (movies == null)) { hobbiesString = books + " , " + anime; } if ((games == null) &amp;&amp; (books == null)) { hobbiesString = movies + " , " + anime; } if ((anime == null) &amp;&amp; (movies == null)) { hobbiesString = games + " , " + books; } if ((anime == null) &amp;&amp; (books == null)) { hobbiesString = games + " , " + movies; } if ((movies == null) &amp;&amp; (books == null)) { hobbiesString = games + " , " + anime; } if ((movies == null) &amp;&amp; (books == null) &amp;&amp; (anime == null)) { hobbiesString = games; } if ((movies == null) &amp;&amp; (games == null) &amp;&amp; (anime == null)) { hobbiesString = books; } if ((games == null) &amp;&amp; (books == null) &amp;&amp; (anime == null)) { hobbiesString = movies; } if ((movies == null) &amp;&amp; (books == null) &amp;&amp; (games == null)) { hobbiesString = anime; } } </code></pre> <p>The Second Screen:</p> <pre><code>private void startUp() { // TODO Auto-generated method stub Intent startUp = getIntent(); String receivedString = startUp.getStringExtra(HOBBIES_STRING); if (receivedString != null) { String[] separated = receivedString.split(","); if (separated.length &gt; 0) { if (separated[0].equals("Anime")) { anime.setChecked(true); } /*----------------*/ if (separated[0].equals("Computer Games")) { games.setChecked(true); } /*----------------*/ if (separated[0].equals("Books")) { books.setChecked(true); } /*----------------*/ if (separated[0].equals("Movies")) { movies.setChecked(true); } /*----------------*/ if (separated[0].equals("Anime")&amp;&amp; separated[0].equals("Computer Games")) { anime.setChecked(true); games.setChecked(true); } if (separated[0].equals("Anime")&amp;&amp;separated[0].equals("Books")) { anime.setChecked(true); books.setChecked(true); } if (separated[0].equals("Anime")&amp;&amp;separated[0].equals("Movies")) { anime.setChecked(true); movies.setChecked(true); } } } </code></pre> <p>The code still doesn't run as expected. As you can see, I have used string split and string arrays, but using arrays makes it hard to handle because of extreme hardcoding and array index bounds problems.</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