2

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

Ganapathy C
  • 5,989
  • 5
  • 42
  • 75
Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100
  • 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). – Antrromet Dec 13 '11 at 09:42

6 Answers6

5

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.

Aman Alam
  • 11,231
  • 7
  • 46
  • 81
2
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.

Jordec
  • 1,516
  • 2
  • 22
  • 43
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
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.


Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
chikka.anddev
  • 9,569
  • 7
  • 38
  • 46
1

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...
0

There are two ways:

  1. Pass it into Intent

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

    ((MyApplication) getApplication()).setJobNo(item.jobNo);
    
Pang
  • 9,564
  • 146
  • 81
  • 122
Vikas
  • 24,082
  • 37
  • 117
  • 159
0

Use Bundle in onClickListner of the listview .

Bundle will pass data from one activity to Next.

Ganapathy C
  • 5,989
  • 5
  • 42
  • 75