[英]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
.我在对象
o
中有数据。 There are three set of values in o
(description, name, image url). o
中有三组值(描述、名称、图像 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
更好的是让你自定义对象
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: ang 在
ShowProduct
Activity 中获取这些值:
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.您不能使用 Intent 将对象从一个活动传递到另一个活动。 Yo have to use Parcelable interface .
哟必须使用 Parcelable 接口。 see this.
看到这个。
http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html 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. 1-创建一个扩展应用程序的类。 2-make an object of what you want to transfer.
2-制作您想要传输的对象。 3-make its set and get.
3-设置并获取。 4-in onitemclick set the value.
4-在onitemclick 中设置值。 5-get the value wherever you want.
5-随时随地获得价值。
Example:例子:
1-make a class which extend Application. 1-创建一个扩展应用程序的类。 2-make an object of what you want to transfer.
2-制作您想要传输的对象。 3-make its set and get.
3-设置并获取。
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(...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.