Search code examples
androidjsonlistviewandroid-intentonitemclicklistener

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


Bit of background, I have a ListView that is made from parsed data. The onItemClickListener is posted in the onPostExecute() of the AsyncTask that does the parsing.

I also have many other URLs to parse, so I use intents to send them over with a click and they get parsed on a separate activity. Different URL depending on what the user clicks. I ran a test and it doesn't matter what the user clicks on they are always getting the intent from the last onclick item. Here it is:

// Click on ListView Row:
     listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                
           switch(position) {
           case 0:
              Intent one = new Intent(MainActivity.this, BookDetails.class);
              one.putExtra("jsonUrl", "url 1 here");
              startActivity(one);
              one.removeExtra("jsonUrl");
           case 1:
              Intent two = new Intent(MainActivity.this, BookDetails.class);
              two.putExtra("jsonUrl", "url 2 here");
              startActivity(two);
              two.removeExtra("jsonUrl");
           case 2:
              Intent three = new Intent(MainActivity.this, BookDetails.class);
              three.putExtra("jsonUrl", "url 3 here");
              startActivity(three);
              three.removeExtra("jsonUrl");
           case 3: 
              Intent four = new Intent(MainActivity.this, BookDetails.class);
              four.putExtra("jsonUrl", "url 4 here");
              startActivity(four);
              four.removeExtra("jsonUrl");
           case 4: 
              Intent five = new Intent(MainActivity.this, BookDetails.class);
              five.putExtra("jsonUrl", "url 5 here");
              startActivity(five);
              five.removeExtra("jsonUrl");
           case 5:
              Intent six = new Intent(MainActivity.this, BookDetails.class);
              six.putExtra("jsonUrl", "url 6 here");
              startActivity(six);
              six.removeExtra("jsonUrl");
           case 6: 
              Intent seven = new Intent(MainActivity.this, BookDetails.class);
              seven.putExtra("jsonUrl", "url 7 here");
              startActivity(seven);
              seven.removeExtra("jsonUrl");
           case 7:
              Intent eight = new Intent(MainActivity.this, BookDetails.class);
              eight.putExtra("jsonUrl", "url 8 here");
              startActivity(eight);
              eight.removeExtra("jsonUrl");
           case 8:
              Intent nine = new Intent(MainActivity.this, BookDetails.class);
              nine.putExtra("jsonUrl", url 9 here);
              startActivity(nine);
              nine.removeExtra("jsonUrl");
                }

Ask me if you need to see more code.

This whole thing is a bit crazy to me, why is it sending only the bottom one when the user clicked something entirely different? The URL gets sent into a new Async, parsed and shows the user data. I am always being show the data of the last URL. This is insane to me because it looks right to me.

Edit: I added removeExtra because I thought it would work, but it didn't. So this happens without that first.

Any help would be greatly appreciated. Thanks.


Solution

  • 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.