java 从另一个活动中获取变量(Android Studio)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32748482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Variable from another Activity (Android Studio)
提问by tobekingforaday
I know that there are a lot of answers to this question already, but I'm still having trouble dealing with this concept.
我知道这个问题已经有很多答案,但我仍然无法处理这个概念。
An answer found hereshows:
此处找到的答案显示:
If you didn't want to use a global variable you could always create a method in your activity to return your string.
如果您不想使用全局变量,您始终可以在您的活动中创建一个方法来返回您的字符串。
public String getMyString(){
return item; }
Then in your current activity you could call:
然后在您当前的活动中,您可以调用:
String myValue = LoginScreen.getMyString();
When I try this method, I am returned an error saying "Non-Static method can not be referenced from a static context". However if I make the methodstatic it says that my variableneeds to be static, but I need to update the variable. I'll include my code below.
当我尝试此方法时,返回一个错误消息,提示“无法从静态上下文中引用非静态方法”。但是,如果我将方法设为静态,则表示我的变量需要是静态的,但我需要更新变量。我将在下面包含我的代码。
First Activity -
第一个活动 -
btnSEARCH.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (editTextSearch.getText().toString() != null) {
SearchQueryTerm = editTextSearch.getText().toString();
editTextSearch.setText("");
}
}
});
public String getMyString(){
return SearchQueryTerm;
}
Second Activity-
第二个活动-
String SearchQueryTerm = MainActivity.getMyString();
I truly appreciate any help you can give me in this. Thanks so much!! <3
我真的很感激你能在这方面给我的任何帮助。非常感谢!!<3
This is my updated code - however it still crashes :(
这是我更新的代码 - 但是它仍然崩溃:(
Activity 1
活动一
public void sendMessageIntent(View view) {
Intent search_intent = new Intent(this, SearchActivity.class);
api_intent.putExtra("my_variable", SearchQueryTerm);
startActivity(search_intent);
}
xml file
xml文件
<Button
android:id="@+id/button_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="View"
android:onClick="sendMessageIntent"
/>
Activity 2 -
活动 2 -
public String SearchQueryTerm
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_variable);
if (extras != null) {
SearchQueryTerm = extras.getString("my_variable");
}
}
回答by Yassine.b
Use putExtra method of an object Intent.
使用对象 Intent 的 putExtra 方法。
In your first activity create for example :
在您的第一个活动中创建例如:
String value = "value";
Intent i = new Intent(getApplicationContext(), MyActivity.class);
i.putExtra("my_variable",value);
startActivity(i);
In your second activity you can retrieve your variable :
在您的第二个活动中,您可以检索您的变量:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("my_variable");
}
It's the best method to pass a variable between activities.
这是在活动之间传递变量的最佳方法。
To show you how it works, i have done this code :
为了向您展示它是如何工作的,我已经完成了以下代码:
XML of first activity :
第一个活动的 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTextSearch"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSearch"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
</RelativeLayout>
First Activity :
第一个活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSEARCH = (Button) findViewById(R.id.buttonSearch);
btnSEARCH.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editTextSearch = (EditText) findViewById(R.id.editTextSearch);
if (editTextSearch.getText().toString() != null) {
String value = "value";
Intent i = new Intent(getApplicationContext(), Main2Activity.class);
i.putExtra("SearchQueryTerm",editTextSearch.getText().toString());
startActivity(i);
editTextSearch.setText("");
}
}
});
}
}
In this first activity we pass the value of the editText in the putExtra method with the key = "SearchQueryTerm".
在第一个活动中,我们使用键 = "SearchQueryTerm" 在 putExtra 方法中传递 editText 的值。
To retrieve the value of editText do this in your second Activity :
要检索 editText 的值,请在第二个 Activity 中执行此操作:
public class Main2Activity extends AppCompatActivity {
String SearchQueryTerm = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle extras = getIntent().getExtras();
if (extras != null) {
SearchQueryTerm = extras.getString("SearchQueryTerm");
}
System.out.println(SearchQueryTerm);
}
}
Now you have the value of your editText in the variable SearchQueryTerm ( Second Activity ).
现在,您在变量 SearchQueryTerm ( Second Activity ) 中拥有了 editText 的值。
It works for me, so there is no reason it dont work for you.
它对我有用,所以没有理由对你不起作用。
Hope it helps.
希望能帮助到你。
回答by Jerome B.
a good solution is to add a "model" singleton class, where you store data that have to be shared between your activities
一个好的解决方案是添加一个“模型”单例类,您可以在其中存储必须在您的活动之间共享的数据
example :
例子 :
public class Model {
private static Model __instance == null;
private Model() {
}
public static Model instance() {
if (__instance == null) {
__instance = new Model();
}
return __instance;
}
private Object mydataToShare = null;
public void setMyDataToShare(Object mydataToShare) {
this.mydataToShare = mydataToShare;
}
public Object getMyDataToShare() {
return mydataToShare;
}
}
}
to store data :
存储数据:
Model.instance().setMyDataToShare(<value to store>);
to retrieve data :
检索数据:
Object valueToRetrieve = Model.instance().getMyDataToShare();
it allow you to transfer complex data and separate completely logic part from UI
它允许您传输复杂的数据并将逻辑部分与 UI 完全分离