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

时间:2021-09-02 08:11:48

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.

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

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

6 个解决方案

#1


3  

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

#2


3  

      Bundle bundle =new Bundle();

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

#3


2  

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

您无法使用Intent将对象从一个活动传递到另一个活动。哟必须使用Parcelable接口。看到这个。

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

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

#4


1  

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.

1 - 创建一个扩展Application的类。 2 - 制作你想要转移的对象。 3 - 制定它并获得。 4-in onitemclick设置值。 5 - 随时随地获取价值。

Example:

例:

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

1 - 创建一个扩展Application的类。 2 - 制作你想要转移的对象。 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.

希望这个帮助。

#5


0  

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

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

#6


0  

You can do it like this:

你可以这样做:

Intent intent = new Intent(YourActivity.this, NewActivity.class);

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

Intent descriptionIntent = getIntent();

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

#1


3  

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

#2


3  

      Bundle bundle =new Bundle();

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

#3


2  

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

您无法使用Intent将对象从一个活动传递到另一个活动。哟必须使用Parcelable接口。看到这个。

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

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

#4


1  

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.

1 - 创建一个扩展Application的类。 2 - 制作你想要转移的对象。 3 - 制定它并获得。 4-in onitemclick设置值。 5 - 随时随地获取价值。

Example:

例:

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

1 - 创建一个扩展Application的类。 2 - 制作你想要转移的对象。 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.

希望这个帮助。

#5


0  

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

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

#6


0  

You can do it like this:

你可以这样做:

Intent intent = new Intent(YourActivity.this, NewActivity.class);

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

Intent descriptionIntent = getIntent();

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