简体   繁体   中英

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);
    }
}

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

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

      Bundle bundle =new Bundle();

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

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.

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");

当你有数据时,你可以使用 Bundle 或者你可以直接使用 Intent 附加数据,例如in.putExtra(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM