3

on the caller activity I have:

 protected void onListItemClick(ListView l, View v, int position, long id) {


          Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
          myIntent.putExtra("categoriaId", String.valueOf(id));
          MainActivity.this.startActivity(myIntent);
        // Toast.makeText(this, String.valueOf(id), Toast.LENGTH_LONG).show();
        }   

the toast return the right value of the id of the item clicked on the list. on the reciver activity i have:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maindomanda);

        int category = getIntent().getExtras().getInt("categoriaId"); 
        TextView text = (TextView) findViewById(R.id.domanda);
        text.setText(String.valueOf(category));
    }

a very straight forward... the text field returns always 0... what i'm doing wrong? is the String.ValueOf() somehow deprecated? or is better to use something else... and another side question... i pass the long id like i know what i'm doing, it's quite false... is it the so called "_ID" field? i get those values from a dbtable, is that that table ID?

Matteo Bononi 'peorthyr'
  • 2,170
  • 8
  • 46
  • 95

3 Answers3

2

Change your code as:

String strcategory = getIntent().getExtras().getString("categoriaId"); 

because you are passing String from onListItemClick to sondaggioActivity Activity and trying to receive as Int

NOTE :

Second and important point is id is long and you are trying to parsing it to Int which is also not valid . see this post: How can I convert a long to int in Java?

Solution is just send id as long from onListItemClick as

 myIntent.putExtra("categoriaId",id);

and receive it as in sondaggioActivity Activity :

long category = getIntent().getExtras().getLong("categoriaId");
Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

You are getting a int getInt("categoriaId"), you passed in a string putExtra("categoriaId", String.valueOf(id)); String.valueOf(); returns a STring representation of the int.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
0

pass categoriaId as long myIntent.putExtra("categoriaId", id);