我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?


当前回答

使用谷歌的Gson库,您可以将object传递给另一个活动。实际上,我们将以json字符串的形式转换对象,传递给其他活动后,我们将再次重新转换为这样的对象

考虑这样一个bean类

 public class Example {
    private int id;
    private String name;

    public Example(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

我们需要传递Example类的对象

Example exampleObject=new Example(1,"hello");
String jsonString = new Gson().toJson(exampleObject);
Intent nextIntent=new Intent(this,NextActivity.class);
nextIntent.putExtra("example",jsonString );
startActivity(nextIntent);

对于读取,我们需要在NextActivity中做相反的操作

 Example defObject=new Example(-1,null);
    //default value to return when example is not available
    String defValue= new Gson().toJson(defObject);
    String jsonString=getIntent().getExtras().getString("example",defValue);
    //passed example object
    Example exampleObject=new Gson().fromJson(jsonString,Example .class);

在gradle中添加这个依赖

compile 'com.google.code.gson:gson:2.6.2'

其他回答

我发现最简单的解决办法是… 创建带有getter和setter的静态数据成员的类。

从一个活动中设置并从另一个活动中获取该对象。

活动

mytestclass.staticfunctionSet("","",""..etc.);

活动b

mytestclass obj= mytestclass.staticfunctionGet();

快速需求的简短回答

1. 将类实现为可序列化的。

如果你有任何内部类,不要忘记将它们实现为Serializable !!

public class SportsData implements  Serializable
public class Sport implements  Serializable

List<Sport> clickedObj;

2. 将你的对象放入Intent中

 Intent intent = new Intent(SportsAct.this, SportSubAct.class);
            intent.putExtra("sport", clickedObj);
            startActivity(intent);

3.并在另一个活动类中接收您的对象

Intent intent = getIntent();
    Sport cust = (Sport) intent.getSerializableExtra("sport");
We can send data one Activty1 to Activity2 with multiple ways like.
1- Intent
2- bundle
3- create an object and send through intent
.................................................
1 - Using intent
Pass the data through intent
Intent intentActivity1 = new Intent(Activity1.this, Activity2.class);
intentActivity1.putExtra("name", "Android");
startActivity(intentActivity1);
Get the data in Activity2 calss
Intent intent = getIntent();
if(intent.hasExtra("name")){
     String userName = getIntent().getStringExtra("name");
}
..................................................
2- Using Bundle
Intent intentActivity1 = new Intent(Activity1.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putExtra("name", "Android");
intentActivity1.putExtra(bundle);
startActivity(bundle);
Get the data in Activity2 calss
Intent intent = getIntent();
if(intent.hasExtra("name")){
     String userName = getIntent().getStringExtra("name");
}
..................................................
3-  Put your Object into Intent
Intent intentActivity1 = new Intent(Activity1.this, Activity2.class);
            intentActivity1.putExtra("myobject", myObject);
            startActivity(intentActivity1); 
 Receive object in the Activity2 Class
Intent intent = getIntent();
    Myobject obj  = (Myobject) intent.getSerializableExtra("myobject");

POJO类“Post”(注意它是实现Serializable的)

package com.example.booklib;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;

public class Post implements Serializable{
    public String message;
    public String bitmap;
    List<Comment> commentList = new ArrayList<Comment>();
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getBitmap() {
        return bitmap;
    }
    public void setBitmap(String bitmap) {
        this.bitmap = bitmap;
    }
    public List<Comment> getCommentList() {
        return commentList;
    }
    public void setCommentList(List<Comment> commentList) {
        this.commentList = commentList;
    }

}

POJO类“Comment”(由于是Post类的成员,它也需要实现Serializable)

    package com.example.booklib;

    import java.io.Serializable;

    public class Comment implements Serializable{
        public String message;
        public String fromName;
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public String getFromName() {
            return fromName;
        }
        public void setFromName(String fromName) {
            this.fromName = fromName;
        }

    }

然后在您的活动类中,您可以执行以下操作将对象传递给另一个活动。

ListView listview = (ListView) findViewById(R.id.post_list);
listview.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Post item = (Post)parent.getItemAtPosition(position);
            Intent intent = new Intent(MainActivity.this,CommentsActivity.class);
            intent.putExtra("post",item);
            startActivity(intent);

        }
    });

在您的接收类“CommentsActivity”中,您可以获得如下数据

Post post =(Post)getIntent().getSerializableExtra("post");
Start another activity from this activity pass parameters via Bundle Object

Intent intent = new Intent(this, YourActivity.class);
Intent.putExtra(AppConstants.EXTRAS.MODEL, cModel);
startActivity(intent);
Retrieve on another activity (YourActivity)

ContentResultData cModel = getIntent().getParcelableExtra(AppConstants.EXTRAS.MODEL);