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);
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);