我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?
当前回答
最简单的java方法是:在你的pojo/model类中实现serializable
推荐用于Android的性能视图:使模型可封装
其他回答
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");
另一种方法是使用Application对象(android.app.Application)。在AndroidManifest.xml文件中定义如下:
<application
android:name=".MyApplication"
...
然后,您可以从任何活动调用它,并将对象保存到Application类。
在FirstActivity中:
MyObject myObject = new MyObject();
MyApplication app = (MyApplication) getApplication();
app.setMyObject(myObject);
在SecondActivity中,执行以下操作:
MyApplication app = (MyApplication) getApplication();
MyObject retrievedObject = app.getMyObject(myObject);
如果你的对象具有应用程序级别的作用域,即它们必须在整个应用程序中使用,这是很方便的。如果您希望显式控制对象范围,或者对象范围是有限的,那么Parcelable方法仍然更好。
不过,这完全避免了intent的使用。我不知道是否适合你。我使用它的另一种方式是让对象的int标识符通过intent发送,并在Application对象中检索我在Maps中拥有的对象。
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);
有几种方法可以访问其他类或Activity中的变量或对象。
答:数据库
B.共同的偏好。
C.对象序列化。
D.可以保存公共数据的类可以命名为common Utilities,这取决于你。
E.通过intent和Parcelable接口传递数据。
这取决于您的项目需求。
答:数据库
SQLite是一个嵌入Android的开源数据库。SQLite支持标准的关系数据库特性,如SQL语法、事务和准备好的语句。
教程——http://www.vogella.com/articles/AndroidSQLite/article.html
B.共享偏好
假设您想存储用户名。现在有两个东西一个键用户名,值值。
如何储存
// Create an object of SharedPreferences.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//now get Editor
SharedPreferences.Editor editor = sharedPref.edit();
//put your value
editor.putString("userName", "stackoverlow");
//commits your edits
editor.commit();
使用putString(),putBoolean(),putInt(),putFloat(),putLong(),您可以保存所需的数据类型。
如何获取
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String userName = sharedPref.getString("userName", "Not Available");
http://developer.android.com/reference/android/content/SharedPreferences.html
C.对象序列化
如果我们想要保存对象状态以便通过网络发送它,则使用对象序列化,或者您也可以将其用于您的目的。
使用java bean并将其存储为他的字段之一,并为此使用getter和setter
JavaBeans是具有属性的Java类。想到的 属性作为私有实例变量。因为它们是私人的,唯一的办法 可以通过类中的方法从类外部访问它们。的 改变属性值的方法称为setter方法,而这些方法称为setter方法 检索属性值的方法称为getter方法。
public class VariableStorage implements Serializable {
private String inString ;
public String getInString() {
return inString;
}
public void setInString(String inString) {
this.inString = inString;
}
}
在邮件方法中通过使用设置该变量
VariableStorage variableStorage = new VariableStorage();
variableStorage.setInString(inString);
然后使用对象序列化序列化此对象,并在其他类中反序列化此对象。
在序列化中,对象可以表示为字节序列,其中包括对象的数据、关于对象类型的信息以及存储在对象中的数据类型。
序列化对象被写入文件后,可以从文件中读取并反序列化它,也就是说,可以使用表示对象及其数据的类型信息和字节在内存中重新创建对象。
如果你想要这方面的教程,请参考这个链接
http://javawithswaranga.blogspot.in/2011/08/serialization-in-java.html
在其他类中获取变量
d . CommonUtilities
你可以自己创建一个类,它可以包含你在项目中经常需要的公共数据。
样本
public class CommonUtilities {
public static String className = "CommonUtilities";
}
E.通过intent传递数据
有关传递数据的选项,请参阅本教程。
http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
我也遇到过同样的问题。我通过使用一个静态类来解决这个问题,在HashMap中存储我想要的任何数据。在上面,我使用了标准活动类的扩展,其中我覆盖了onCreate和onDestroy方法来做数据传输和数据清除隐藏。一些荒谬的设置必须改变,例如方向处理。
注释: 不提供一般对象传递给另一个活动是痛苦的屁股。这就像射击自己的膝盖,希望赢得100米。“比喻”并不是一个充分的替代品。这让我笑了……我不想实现这个接口到我的技术自由API,因为我更不想引入一个新的层…这是怎么回事,我们在移动编程中离现代范式如此之远……
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的