i created simple example in android that let you enter Name of book,author of the book and number of this book i add on click event to the button and this is the code for main activity to send info to second activity.
this is the first activity code whole class.
package com.test2;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
public class Test2Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void OnAddClick(View button)
{
AutoCompleteTextView Name = (AutoCompleteTextView) findViewById(R.id.Name);
AutoCompleteTextView Author = (AutoCompleteTextView) findViewById(R.id.Author);
AutoCompleteTextView Number = (AutoCompleteTextView) findViewById(R.id.Number);
Intent intent=new Intent();
intent.setClass(this,Result.class);
intent.putExtra("BookTitle", Name.getText().toString());
intent.putExtra("AuthorName",Author.getText().toString());
intent.putExtra("Number",Number.getText().toString());
startActivity(intent);
}
public void OnCancelClick(View button)
{
Intent intent=new Intent();
intent.setComponent(new ComponentName(this,Result.class));
intent.putExtra("Cancel", "Cancel");
startActivity(intent);
}
}
and this the code that will recived the intent (the whole class).
package com.test2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Result extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView resultText=(TextView) findViewById(R.id.resultText);
Bundle bundle=getIntent().getExtras();
if(bundle.getString("Cancel")!=null)
{
resultText.setText("Operation Cancelled");
}
else
{
String BookTitle=bundle.getString("BookTitle");
String BookAuthor=bundle.getString("AuthorName");
String BookNumber=bundle.getString("Number");
resultText.setText(
"the result is"+ BookTitle + "-" + BookAuthor + "-" + BookNumber);
}
}
}
and this button xml code:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.11" android:text="Add" android:onClick="OnAddClick"></Button>
when i run it it give unexpected error for add button , and for cancel button it work normally. what i miss in the code ? thanks.