0

I am trying to pull in a variable from one activity intent into another activity and set a textview with it. Here is some code:

//First Activity
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    Cursor c = mNotesCursor;
    c.moveToPosition(position);
    Intent i = new Intent(this, showID.class);
    i.putExtra(HDWDBHelper.KEY_ROWID, id);
    myId = String.valueOf(id);
    i.putExtra(myId, id);
    Log.v("ID: ", myId);

    startActivityForResult(i, ACTIVITY_SHOW);


}

//Second Activity

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showid);

    idNumber = (TextView) findViewById(R.id.textView1);
    Intent myIntent = getIntent();
    Bundle b = myIntent.getExtras();
    Long id = b.getLong("id");
    String myId = String.valueOf(id);
    Log.v("ShowID: ", myId);
    idNumber.setText(myId.toString());

}

I can not see what I am doing wrong.

Garling Beard
  • 101
  • 3
  • 14

3 Answers3

0
myId = String.valueOf(id);
    i.putExtra(myId, id);

You're using the id value as key.

myId = "id";

that should work

edit*: if what you want is to pass a string, then

myId = String.valueOf(id);
    i.putExtra(myId, "id");

and then receive it

String myId = b.getString("id");
Pablo_Cassinerio
  • 887
  • 5
  • 13
0

You can pass primitive data from one activity to another as follows.

Define constant which will be used as Key to pass data.

public static final String EXTRA_YOUR_KEY = "EXTRA_YOUR_KEY";

Add the value as key-pair to the Intent which you are using to start new activity.

// code snippet for first activity
long myId = 10;
Intent activityIntent = new Intent(activity-context, ClassNameOfNewActivity.class);
activityIntent.putExtra(EXTRA_YOUR_KEY, myId);
startActivity(activityIntent);

Next, in the onCreate() of new activity, extract the passed value as

// code snippet from second activity's onCreate method
Intent intent = getIntent();
long id = intent.getLongExtra(ClassNameWhichContains.EXTRA_YOUR_KEY, 0);

I have used long as example since you are trying to pass in a long variable. Similarly, you can work out the other primitives.

And for passing custom objects between two activities you can refer this.

Hope this helps.

Community
  • 1
  • 1
jagmohan
  • 2,052
  • 2
  • 26
  • 41
  • Thanks everyone for their help, I used singh.jagmohan's example and got it to work. – Garling Beard Dec 27 '13 at 16:25
  • 1
    @GarlingBeard : One thing I will say is don't put the `static final` key in a class which extends `Activity`. Create a separate class for constants and/or any static methods you need. – Squonk Dec 27 '13 at 16:50
0

As mentioned above, the extras are actually key value pairs and thus when inserting into extras you have to use "key" which is known to receiving side to be able to pull that value from the bundle by that name. Unfortunately the key could be anything even what you have used thare but that value is not what you know on receiver side. Try this-

Sending side:

i.putExtra("id", id);

Receiving side:

Long id = b.getLong("id");
Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
pxlinux
  • 34
  • 1
  • 2