ListView is sending strings to another Activity, but it always sends the last one only... how do I fix this?

StackOverflow https://stackoverflow.com/questions/19416449

Вопрос

EDIT: Turns out I just didn't put break; between cases so I've removed my code since it was a basic error.

Это было полезно?

Решение

You have forgotten the break statements

switch(position) {
       case 0:
          Intent one = new Intent(MainActivity.this, BookDetails.class);
          one.putExtra("jsonUrl", "url 1 here");
          startActivity(one);
          one.removeExtra("jsonUrl");
          break;          // here
       case 1:
          Intent two = new Intent(MainActivity.this, BookDetails.class);
          two.putExtra("jsonUrl", "url 2 here");
          startActivity(two);
          two.removeExtra("jsonUrl");
          break;      // here
       case 2:
          Intent three = new Intent(MainActivity.this, BookDetails.class);
          three.putExtra("jsonUrl", "url 3 here");
          startActivity(three);
          three.removeExtra("jsonUrl");
          break;         // here

Without breaking out of the switch the statements can just go through to the next condition.

Другие советы

you are not using the break on the switch.

 switch(position) {
           case 0:
              Intent one = new Intent(MainActivity.this, BookDetails.class);
              one.putExtra("jsonUrl", "url 1 here");
              startActivity(one);
              one.removeExtra("jsonUrl");
              **break;**
           case 1:
              Intent two = new Intent(MainActivity.this, BookDetails.class);
              two.putExtra("jsonUrl", "url 2 here");
              startActivity(two);
              two.removeExtra("jsonUrl");
              **break;**

....

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top