user1047442
user1047442

Reputation: 49

How to pass data from a selected item in a listview to another activity?

I have the following code. I have the data in the object o. There are three set of values in o (description, name, image url). I need these data to be initialized to a string and pass it to other activity using intent. How do I get each value in the object. Each list item has a image, item name and item description.

fp_list.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView parentView, View v, int position, long id) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "pos"+position, Toast.LENGTH_LONG).show();
        Object o = parentView.getItemAtPosition(position);
        Intent i = new Intent(FeaturedProductsActivity.this, ShowProduct.class);
    }
}

Upvotes: 1

Views: 13764

Answers (6)

Diana Szczepankowska
Diana Szczepankowska

Reputation: 41

You can do it like this:

Intent intent = new Intent(YourActivity.this, NewActivity.class);
intent.putExtra("description", o.get(position).getdescription())
startActivity(descriptionIntent);

And in the second activity:

Intent descriptionIntent = getIntent();
String description = descriptionIntent.getExtras().getString("description");

Upvotes: 0

Bishoy Fakhry
Bishoy Fakhry

Reputation: 161

simple way :

1-make a class which extend Application. 2-make an object of what you want to transfer. 3-make its set and get. 4-in onitemclick set the value. 5-get the value wherever you want.

Example:

1-make a class which extend Application. 2-make an object of what you want to transfer. 3-make its set and get.

public class App extends Application {
private static yourObject  obj;

public yourObject getobj() {
return obj;
}
public void setobj(yourObject obj) {
this.obj= obj;
}
}

in your class

fp_list.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
public void onItemSelected(AdapterView parentView, View v, int position, long id) {
    // TODO Auto-generated method stub

    yourObject  o =(yourObject) parentView.getItemAtPosition(position);
   App.setobj(o);
}

}

in any other where all over the application classes

//here you will get your object which you have set on item click
yourObject obj=App.getobj;

hope this help.

Upvotes: 1

Hardik Gajjar
Hardik Gajjar

Reputation: 5058

      Bundle bundle =new Bundle();

                bundle.putString("memId",o.getId() );
                Intent newIntent=new Intent(friendsOffrinends.this,RestaurantDetails.class);
                newIntent.putExtras(bundle);
                startActivityForResult(newIntent, 0);

Upvotes: 2

Khawar
Khawar

Reputation: 5227

When you have data you can use Bundle or you can directly attach data with Intent e.g. in.putExtra(...).

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

Better thing is to make you Custom Object Parcelable

the Other option is to pass these values one by one as:

Intent i=new Intent(FeaturedProductsActivity.this,ShowProduct.class);
i.putString("desc", o.getDescription());
i.putString("name", o.getName());
// rest of your values

ang get these values in ShowProduct Activity as:

Intent in = this.getIntent();
String name = in.getStringExtra("name");
String desc = in.getStringExtra("desc");
//rest of you values

Upvotes: 3

Sujit
Sujit

Reputation: 10622

You can't pass object from One activity to another using Intent. Yo have to use Parcelable interface . see this.

http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html

Upvotes: 2

Related Questions