Shishir.bobby
Shishir.bobby

Reputation: 10984

passing data from list view to another activity

i want to create an activity, in which, i would like to have a listview, there can be about 20-30 items in a list view,on tapping any particular value in listview, it should move to another activity, with the data of list view.

Just wanna pass data from listview to another activity. Suggestion plz Regards

Upvotes: 2

Views: 33292

Answers (6)

Farooq Khan
Farooq Khan

Reputation: 21

String selectedItem =arrayAdapter.getItem(position);

            Intent intent = new Intent(getApplicationContext(), 
            Your_Second_Activity.class);
            intent.putExtra("selectedItem", selectedItem);
            startActivity(intent);

           Second_Activity.Class
           Bundle bundle = getIntent().getExtras();
           String yourItem = bundle.getString("selectedItem");

  Now! your selected item is inside in the yourItem Variable...

Upvotes: 1

Tanmay Mandal
Tanmay Mandal

Reputation: 40168

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

   Object obj = this.getListAdapter().getItem(position);
   String value= obj.toString();

   Intent intent= new Intent(CurrrentClass.this,NextClass.class);
   intent.putExtra("value", value);                 
   startActivity(intent);    
}

Hope this will help you.

Upvotes: 2

Vikas
Vikas

Reputation: 24332

There are two ways:

  1. Pass it into Intent

    intent.putExtra("jobNo", item.jobNo);
    
  2. Use Application scope

    ((MyApplication) getApplication()).setJobNo(item.jobNo);
    

Upvotes: 0

chikka.anddev
chikka.anddev

Reputation: 9629

You can pass data from one activity to another activity:


see this link


and to get data for ListView you have to first implement getListView.setOnItemClickListener(), and have to get position of item in ListView and use the index to get data form your adapter from where you are binding data to ListView.


Upvotes: 2

Aman Aalam
Aman Aalam

Reputation: 11251

Implement ListView's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked.

Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.

Now use this code:

Intent anotherActivityIntent = new Intent(this, AnotherActivity.class);
anotherActivityIntent.putExtra("my.package.dataToPass",dataFromClickedRow);
startActivity(anotherActivityIntent);

and when the anotherActivityIntent starts the AnotherActivity class, use following code to access the value that you had passed:

Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("my.package.dataToPass");

Now you have your data in myVal variable. you can use any other data type, whichever you like.

Upvotes: 5

Ganapathy C
Ganapathy C

Reputation: 5999

Use Bundle in onClickListner of the listview .

Bundle will pass data from one activity to Next.

Upvotes: 0

Related Questions