这听起来简单,但我不能让它工作。我有两个活动。其中第一个是一个形式,并且第二个被示出的数据从一个JSON文件的基础上在所述第一活动输入的值。
It might sound simple but I can't make it working. I have two activities. Which first one is a form, and second one is showing data from a JSON file based on values entered in the first activity.
所以,我想使一个简单的版本。我有一个的EditText,和一个按钮,所以当他们preSS的按钮,无论是在的EditText将在下一个活动的TextView的
So I am trying to make a simple version of it. I have a EditText, and a button, so when they press the button, whatever was in the EditText will be in the TextView of the next activity.
下面是我的code到目前为止: 主要活动
Here is my code so far: Main Activity
static TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.editText1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void transferIT(View view){
Intent intent = new Intent(this, Page.class);
startActivity(intent);
}
public static String getText(){
String data = (String) textView.getText();
return data;
}
主要XML
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter Text to transfer" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Transfer it"
android:onClick="transferIT" />
第二项活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page);
// Show the Up button in the action bar.
setupActionBar();
TextView enteredValue = (TextView)findViewById(R.id.text);
enteredValue.setText(MainActivity.getText());
}
第二个XML
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp" />
在这种方式我犯了一个方法,为的EditText的数据和我调用它来访问它的其他活动。目前,当我preSS的按钮,它停止工作。那么,什么是您的解决方案具有在其他活动形式的数据?
In this way I made a method for the data of the EditText and in the other activity I call it to get access to it. Currently when I press the button it stops working. So what is your solution for having the data of the form in other activity ?
在第一个活动,你应该把多余的参数意图是这样的:
In first activity you should put extra argument to intent like this:
// I assume Page.class is your second ativity
Intent intent = new Intent(this, Page.class);
intent.putExtra("arg", getText()); // getText() SHOULD NOT be static!!!
startActivity(intent);
然后在第二个活动,你retrive的说法是这样的:
Then in second activity you retrive argument like this:
String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);
这也是很好的保存ARG字符串中MainActivity为常数,总是指它在其他地方。
It's also good to store "arg" String in MainActivity as constant and always refer to it in other places.
public static final String ARG_FROM_MAIN = "arg";