0

Okay so i have a class called viewList. When the item it long pressed the id is looked up of that item in my SQL database. And starts a new activity. How would i transfer this id that is retrieved on long press, to my new activity so it can intern look up the info.

case R.id.lookup_book:
    AdapterContextMenuInfo web = 
        (AdapterContextMenuInfo)item.getMenuInfo();

            mDbHelper.fetchinfo(web.id);
    Intent v = new Intent(this, bookLookup.class);
    startActivity(v);

I would like to pass the web.id to this activity

public class bookLookup extends Activity {
WebView webView;
private DbAdapter mDbHelper;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.book_browser);
    webView = (WebView)findViewById(R.id.webkit);



}

}

tj walker
  • 1,333
  • 3
  • 15
  • 18

3 Answers3

3

When launching an activity, you can pass extras to the intent

Intent bookLookup = new Intent(this, bookLookup.class);
bookLookup.putExtra("id", "123");
startActivity(bookLookup); 

The Activity that was launched can pick it up using getExtra :

Intent myIntent = getIntent(); 
myIntent.getExtra("id");
ddewaele
  • 22,363
  • 10
  • 69
  • 82
1

In your longClickListener add a metgod to put extra data into the intent. It can be done in this way

Intent intent = new Intent(this, bookLookup.class);
intent.putExtra("myId", id);
startActivity(v);

In the new activity you can retrieve this extra value by using this

Bundle extras = getIntent().getExtras();
int id = extras != null ? extras.getLong("myId") : -1;

Take care that the myId string that I used to pass and retrieve values between activities should be the same. You can pass multiple items in this way by giving a unique name for each item and use the same name to retrieve them.

You can pass any primitive datatypes in this way. I used long as an example.

achie
  • 4,716
  • 7
  • 45
  • 59
0

see Intent send is not intent received

Community
  • 1
  • 1
sunriser
  • 770
  • 3
  • 12