我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?
当前回答
我发现最简单的解决办法是… 创建带有getter和setter的静态数据成员的类。
从一个活动中设置并从另一个活动中获取该对象。
活动
mytestclass.staticfunctionSet("","",""..etc.);
活动b
mytestclass obj= mytestclass.staticfunctionGet();
其他回答
从这个活动中启动另一个活动,通过Bundle对象传递参数
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("USER_NAME", "xyz@gmail.com");
startActivity(intent);
检索另一个活动(YourActivity)
String s = getIntent().getStringExtra("USER_NAME");
这对于简单类型数据类型是可以的。 但是如果你想在活动之间传递复杂的数据,你需要先序列化它。
这里我们有员工模型
class Employee{
private String empId;
private int age;
print Double salary;
getters...
setters...
}
可以使用谷歌提供的Gson lib对复杂数据进行序列化 像这样
String strEmp = new Gson().toJson(emp);
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("EMP", strEmp);
startActivity(intent);
Bundle bundle = getIntent().getExtras();
String empStr = bundle.getString("EMP");
Gson gson = new Gson();
Type type = new TypeToken<Employee>() {
}.getType();
Employee selectedEmp = gson.fromJson(empStr, type);
迄今为止最简单的方法IMHO包裹对象。只需在希望可打包的对象上方添加注释标记。
下面是该库的一个示例https://github.com/johncarl81/parceler
@Parcel
public class Example {
String name;
int age;
public Example(){ /*Required empty bean constructor*/ }
public Example(int age, String name) {
this.age = age;
this.name = name;
}
public String getName() { return name; }
public int getAge() { return age; }
}
我知道有点晚了,但很简单。你所要做的就是让你的类实现Serializable
public class MyClass implements Serializable{
}
然后你可以传递给一个意图,比如
Intent intent=......
MyClass obje=new MyClass();
intent.putExtra("someStringHere",obje);
要得到它,你只需打电话
MyClass objec=(MyClass)intent.getExtra("theString");
有几种方法可以访问其他类或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/
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);
推荐文章
- 警告: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文件