android - 如何将数据从 ListView 中的选定项目传递到另一个 Activity ?

标签 android listview

我有以下代码。我在对象 o 中有数据。 o 中有三组值(描述、名称、图片 url)。我需要将这些数据初始化为字符串并使用 intent 将其传递给其他 Activity 。如何获取对象中的每个值。每个列表项都有一个图像、项目名称和项目描述。

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

最佳答案

更好的办法是让你自定义对象 Parcelable

另一个选项是将这些值一一传递为:

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

ShowProduct Activity 中获取这些值作为:

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

关于android - 如何将数据从 ListView 中的选定项目传递到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8352467/

相关文章:

java - Intent 的 RecyclerView 中未检测到 OnClick

android - 将 Cordova ionic 项目导入 Android studio?

java - 安卓 : marquee on listview don't work

android - 从listview android中删除了错误的行

java - 如何将 Android Wifi 扫描结果添加到列表中?

android - 临时绕过ListView滚动

android - React native 构建失败 : The development server returned response error code 500

Android 应用程序设计分辨率

c# - 为什么 foreach 在从 ListView 中删除项目时起作用,而在 ListBox 中不起作用?

.net - 检查 CheckedListBox 中的项目而不选择