我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?
当前回答
首先在类中实现Parcelable。然后像这样传递object。
SendActivity.java
ObjectA obj = new ObjectA();
// Set values etc.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.package.ObjectA", obj);
startActivity(i);
ReceiveActivity.java
Bundle b = getIntent().getExtras();
ObjectA obj = b.getParcelable("com.package.ObjectA");
包字符串不是必须的,只是在两个活动中字符串需要相同
参考
其他回答
最简单的java方法是:在你的pojo/model类中实现serializable
推荐用于Android的性能视图:使模型可封装
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");
你可以使用android BUNDLE来做到这一点。
从你的类中创建一个Bundle,像这样:
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString("SomeKey", "SomeValue");
return b;
}
然后用INTENT传递这个bundle。 现在你可以通过传递bundle来重新创建你的类对象
public CustomClass(Context _context, Bundle b) {
context = _context;
classMember = b.getString("SomeKey");
}
在自定义类中声明并使用。
对于你知道要在应用程序中传递数据的情况,使用“全局变量”(比如静态类)
以下是Dianne Hackborn (hackbod -谷歌安卓软件工程师)对此事的看法:
For situations where you know the activities are running in the same process, you can just share data through globals. For example, you could have a global HashMap<String, WeakReference<MyInterpreterState>> and when you make a new MyInterpreterState come up with a unique name for it and put it in the hash map; to send that state to another activity, simply put the unique name into the hash map and when the second activity is started it can retrieve the MyInterpreterState from the hash map with the name it receives.
你可以通过intent发送可序列化对象
// send where details is object
ClassName details = new ClassName();
Intent i = new Intent(context, EditActivity.class);
i.putExtra("Editing", details);
startActivity(i);
//receive
ClassName model = (ClassName) getIntent().getSerializableExtra("Editing");
And
Class ClassName implements Serializable {
}
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件