【发布时间】:2011-02-11 10:06:12
【问题描述】:
我希望能够将数据从一项活动传输到另一项活动。如何做到这一点?
【问题讨论】:
-
到目前为止你尝试过什么代码?什么不工作?
-
我猜您并没有尝试传输 ZIP 文件。看看这里stackoverflow.com/questions/4872856/…
我希望能够将数据从一项活动传输到另一项活动。如何做到这一点?
【问题讨论】:
通过下面的代码,我们可以在活动之间发送值
在父活动中使用以下代码
Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);
在子活动中使用以下代码
String s= getIntent().getStringExtra(<StringName>);
【讨论】:
String s= getIntent().getStringExtra("StringName"); 工作。
您可以通过多种方式访问其他类或 Activity 中的变量或对象。
A.数据库
B.共享偏好。
C.对象序列化。
D.可以保存公共数据的类可以命名为 Common Utilities,这取决于您。
E.通过 Intents 和 Parcelable 接口传递数据。
这取决于您的项目需求。
A. 数据库
SQLite 是一个嵌入到 Android 中的开源数据库。 SQLite 支持标准的关系数据库功能,如 SQL 语法、事务和准备好的语句。
教程 -- http://www.vogella.com/articles/AndroidSQLite/article.html
B. 共享偏好
假设您要存储用户名。所以现在会有两个东西一个Key用户名,ValueValue。
如何储存
// Create 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
JavaBean 是具有属性的 Java 类。考虑到 属性作为私有实例变量。既然他们是私人的,唯一的办法 可以通过类中的方法从类外部访问它们。这 改变属性值的方法称为 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 传递数据
有关此数据传递选项,请参阅本教程。
【讨论】:
当您将数据从一个活动传递到另一个活动时,执行如下方式
在父活动中:
startActivity(new Intent(presentActivity.this, NextActivity.class).putExtra("KEY_StringName",ValueData));
或像下面父活动中显示的那样
Intent intent = new Intent(presentActivity.this,NextActivity.class);
intent.putExtra("KEY_StringName", name);
intent.putExtra("KEY_StringName1", name1);
startActivity(intent);
在子Activity中执行如下图
TextView tv = ((TextView)findViewById(R.id.textViewID))
tv.setText(getIntent().getStringExtra("KEY_StringName"));
或在子 Activity 中执行如下所示的操作
TextView tv = ((TextView)findViewById(R.id.textViewID));
TextView tv1 = ((TextView)findViewById(R.id.textViewID1))
/* Get values from Intent */
Intent intent = getIntent();
String name = intent.getStringExtra("KEY_StringName");
String name1 = intent.getStringExtra("KEY_StringName1");
tv.setText(name);
tv.setText(name1);
【讨论】:
在 android 中将数据从一个活动传递到另一个活动
Intent intent = new Intent(context, YourActivityClass.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);
从 android 活动中检索捆绑数据
Intent intent = getIntent();
if (intent!=null) {
String stringData= intent.getStringExtra(KEY);
int numberData = intent.getIntExtra(KEY, defaultValue);
boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
char charData = intent.getCharExtra(KEY, defaultValue); }
【讨论】:
【讨论】:
你只需要在调用你的意图时发送额外的东西
像这样:
Intent 意图 = new Intent(getApplicationContext(), SecondActivity.class); intent.putExtra("变量名","你要传递的值");开始活动(意图);
现在在 SecondActivity 的 OnCreate 方法上,您可以像这样获取额外内容
如果你发送的值是“long”
long value = getIntent().getLongExtra("你作为额外发送的变量名", defaultValue(你可以给它任何东西));
如果你发送的值是一个“字符串”
String value = getIntent().getStringExtra("您作为额外发送的变量名称");
如果你发送的值是“布尔值”
Boolean value = getIntent().getStringExtra("您作为额外发送的变量名称",defaultValue);
【讨论】:
假设您想从活动 A 转到活动 B。
所以我们使用 Intent 来切换活动
典型代码如下所示 -
//// Create a New Intent object
Intent i = new Intent(getApplicationContext(), B.class);
/// add what you want to pass from activity A to Activity B
i.putExtra("key", "value");
/// start the intent
startActivity(i);
并从子活动
中获取数据Intent i = getIntent();
if (i!=null) {
String stringData= i.getStringExtra("key");
}
【讨论】:
这样效果最好:
通过下面的代码,我们可以在活动之间发送值
在父活动中使用以下代码(父类/值发送类)
Intent myintent=new Intent(<PARENTCLASSNAMEHERE>.this,<TARGETCLASSNAMEHERE>.class).putExtra("<StringName>", value);
startActivity(myintent);
在子活动中使用以下代码(目标类/活动)
String s= getIntent().getStringExtra(<StringName>);
请看这里,“StringName”是目标/子活动捕获的名称,而“value”是变量名称,与父/目标/发送类中的相同。
【讨论】: