Reputation: 33
Here I have created 2 Activities in my project.
public class CheckAvailability extends Activity{
Button but1,but2;
EditText brName;
TextView txt1;
String text;
//private static final String ATM_NO = "atmbrno";
//private static final String ATM_PLACE = "atmbrname";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.availability);
brName =(EditText)findViewById(R.id.editText1);
but1 = (Button)findViewById(R.id.button5);
but2 = (Button)findViewById(R.id.button6);
but1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String b_name=brName.getText().toString();
Intent intent1 = new Intent();
intent1.setClass(getApplicationContext(), ListAtmActivity.class);
Bundle b = new Bundle();
b.putString("key", b_name);
intent1.putExtras(b);
startActivity(intent1);
finish();}
});
but2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent1 = new Intent();
intent1.setClass(getApplicationContext(), SelectOption.class);
startActivity(intent1);
} });
}
}
And I want to pass b_name String value to the other Activity. and here is my 2nd Activity.
public class ListAtmActivity extends ListActivity{
TextView error;
String brName;
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
Bundle b = getIntent().getExtras();
brName = b.getString("key");
error = (TextView)findViewById(R.id.name);
error.setText(brName);}}
But I cant get that string in my 2nd activity? Please help me to find the error?
Upvotes: 0
Views: 4373
Reputation: 139
On your second activity:
Replace:
Bundle b = getIntent().getExtras();
brName = b.getString("key");
With:
Intent intentListatmactivity = getIntent();
brName = intentListatmactivity.getStringExtra("key");
Upvotes: 0
Reputation: 145
In the first activity
Intent in=new Intent(first.this,second.class);
in.putExtra("nameOfImage", "firstImage");
startActivity(in);
in the second activtiy
Intent intent = getIntent();
String nameOfImage = intent.getStringExtra("nameOfImage");
Upvotes: 0
Reputation: 340
In your first activity
Intent intent = new Intent(context, second.class);
intent.putExtra("string_name", "string");
startActivity(intent);
In your second activity
Bundle bundle = getIntent().getExtras();
extras.getString("string_name")
Upvotes: 0
Reputation: 2301
you need use following code for putExtra()
Intent intent=new Intent(this,ListAtmActivity.class);
intent.putExtra("key",b_name);
startActivity(intent)
And in ListAtmActivity
get string using
String data;
data=getIntent.getExtras().getString("key");
now your sent value in data variable.
Upvotes: 1
Reputation: 12134
In your First Activity,
Intent passIntent = new Intent(FirstActivity.this,
SecondActivity.class);
passIntent.putExtra("key", b_name);
startActivity(passIntent);
In your SecondActivity,
String u_name=getIntent().getExtras().getString("key");
Upvotes: 1
Reputation: 6141
In activity 1:
Intent intent1 = new Intent();
intent1.setClass(getApplicationContext(), ListAtmActivity.class);
intent1.putExtra("key", b_name);
startActivity(intent1);
finish();
In activity 2:
Intent intent = getIntent();
brName = intent.getStringExtra("key");
Upvotes: 1
Reputation: 11992
The answer is simpler than what you tried to do :
Intent intent = new Intent(this, ListAtmActivity.class);
intent.putExtra("key", b_name);
startActivity(intent);
then in activity B :
Intent intent= getIntent(); // gets the previously created intent
String value = intent.getStringExtra("key");
Upvotes: 4
Reputation: 15973
public void onClick(View v){
Intent intent1 = new Intent();
intent1.setClass(CheckAvailability.this , ListAtmActivity.class);
intent1.putExtra("name", b_name );
startActivity(intent1);
}
then in the 2nd activity:
getIntent().getStringExtra("name");
Upvotes: 1