passing data from list view to another activity

31,960

Solution 1

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.

Solution 2

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.


Solution 3

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.

Solution 4

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...
Share:
31,960

Related videos on Youtube

Shishir.bobby
Author by

Shishir.bobby

From Java to Android to iOS App Developer to Project Manager(now).

Updated on July 18, 2020

Comments

  • Shishir.bobby
    Shishir.bobby almost 4 years

    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

    • Antrromet
      Antrromet over 12 years
      I was wondering whether you found the answer or not? Even i am facing the same problem. I want to pass a string from a list view to the activity (this is the same activity that calls the listview).