简体   繁体   English

如何将列表视图中选定项目的数据传递给另一个活动?

[英]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.

相关问题 如何在下一个活动中将选定的列表视图项传递给另一个列表视图 - How to pass selected listview item to another listview in the next activity 如何将Listview选定的项目值传递给另一个活动 - How to pass Listview selected item values to another activity 如何从一个活动的列表视图传递数据并将其传递给另一活动? - How to pass the data from listview of one activity and pass it to another activity? 如何将值从json列表视图中的选定项目发送到另一个活动? - How to send values from a selected item on a json listview to another activity? 如何使用contextMenu将值传递给listview中项目的另一个活动 - How to pass the value to another activity of an item from listview using contextMenu 将名称从列表视图项传递到另一个活动 - Pass the name from listview item to another activity 将列表项中的选定项目数据从当前活动传递到下一个活动 - Pass selected item data from ListView from current activity to next activity 如何将ListView项目的文本传递给另一个活动? - How to pass text of ListView item to another activity? 如何将数据从列表视图传递到另一个活动页面? - how to pass data from listview to another activity page? 如何从列表视图获取数据并将其传递给另一个活动 - How to obtain data from a listview and pass it to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM