Reputation: 3
There is any activity with an input field, it calls the second activity with ListView. I need to get the value from the ListView and return it to the input field in the first activity. Can not return a value, but the activity switches without errors.
MainActivity
edTAirportchoice1 = (EditText) findViewById(R.id.edText);
edText.setOnClickListener(this);
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListActivity.class);
startActivityForResult(intent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null) {
return;
} String name = getIntent().getStringExtra("name");
edText.setText(name);
}
}
ListActivity
list1 = (ListView) findViewById(R.id.list1);
list1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, s1));
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), s1[position], Toast.LENGTH_LONG).show();
Intent intent = new Intent(ListActivity.this, MainActivity.class));
intent.putExtra("name", s1[position]);
setResult(1,intent);
finish();
public void onClick(View v) {
Intent intent = new Intent(ListActivity.this, MainActivity.class);
intent.putExtra("name", edText.getText().toString()); //("name", list1.getOnItemClickListener().toString())- the same result
startActivity(intent);
Upvotes: 0
Views: 117
Reputation: 1800
The right way of sharing some data between 2 activities is:
public static final int REQUEST_LIST = 1;
// Call this to show ListActivity
Intent intent = new Intent(MainActivity.this, ListActivity.class);
startActivityForResult(intent, REQUEST_LIST);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch(requestCode) {
case REQUEST_LIST:
if (data == null) return;
String name = getIntent().getStringExtra("name");
edText.setText(name);
break;
default:
break;
}
}
}
Then in ListActivity you need:
list1 = (ListView) findViewById(R.id.list1);
list1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, s1));
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent resultIntent = new Intent();
resultIntent.putExtra("name", s1[position]);
setResult(RESULT_OK, resultIntent);
finish();
});
Upvotes: 0
Reputation: 22955
Use Intent for this purpose,
From ListActivity
to MainActivity
public void onClick(View v) {
Intent intent = new Intent(ListActivity.this, MainActivity.class);
intent.putExtra("name", list1.getOnItemClickListener().toString());
startActivity(intent);
}
Now in MainActivity
to get this value of name ,
String name= getIntent().getStringExtra("name");
Now to set value in your textview of MainActivity
,
txtView.setText(name);
Upvotes: 0
Reputation: 180
Intent intent = new Intent();
intent.putExtra("name", selectedResult);
setResult(1, intent);
Upvotes: 3