i actually take this code form vogella.com.
i want to pass the data from the second activity to first activity when click the button in second activity. to make sure the value passing is correct, i want it display as a toast when going back to first activity.
this is my code on the firstactivity_layout. res/activity_intentintent.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="60dip"
android:text="First Activity. Press button to call second activity"
android:textSize="20sp" >
</TextView>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Calling an intent" >
</Button>
</LinearLayout>
this is the second code for layout. res/picklayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/input11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/input22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
this is the activity code for the first layout, IntentintentActivity.java
public class IntentintentActivity extends Activity {
Button btn1;
private static final int REQUEST_CODE = 10;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentintent);
}
public void onClick(View view) {
Intent i = new Intent(this, PickActivity.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
// Set the request code to any code you like, you can identify the
// callback via this code
startActivityForResult(i, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("returnKey1")) {
Toast.makeText(this, data.getExtras().getString("returnKey1"),
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_intentintent, menu);
return true;
}
and the last is the second activity, PickActivity.java
public class PickActivity extends Activity{
Button submit;
EditText text1, text2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.picklayout);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
text1 = (EditText) findViewById(R.id.input11);
text2 = (EditText) findViewById(R.id.input22);
text1.setText(value1);
text2.setText(value2);
}
submit = (Button)findViewById(R.id.button1);
/*
submit.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(this, text1.getText(), 2500).show();
}
});
*/
}
public void onClick(View v) {
Toast.makeText(this, text1.getText(), 2500).show();
finish();
}
@Override
public void finish() {
Intent data = new Intent(this, IntentintentActivity.class);
Toast.makeText(this, "finish state", 2500).show();
// Return some hard-coded values
data.putExtra("returnKey1", "Swinging on a star. ");
data.putExtra("returnKey2", "You could be better then you are. ");
setResult(RESULT_OK, data);
int result = 1;
//startActivityForResult(data, result);
super.finish();
}
}
i tried to put listener for the button in the second activity like this.
public class PickActivity extends Activity{
Button submit;
EditText text1, text2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.picklayout);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
String value2 = extras.getString("Value2");
if (value1 != null && value2 != null) {
text1 = (EditText) findViewById(R.id.input11);
text2 = (EditText) findViewById(R.id.input22);
text1.setText(value1);
text2.setText(value2);
}
submit = (Button)findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(this, text1.getText(), 2500).show();
}
});
}
however it said Toast.makeText(...) is not applicable for the argument(new View.onClickListener).
does anyone know how to solve this please. thank you.
In your Toast you have to replace ‘this’ argument with getBaseContext()