0

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);
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
l2win36
  • 3
  • 5
  • In OnItemClick don't start again the activity, use setResult instead. Also the selectedREsult is a Serializable, and you are expecting a string... – Chol Jan 19 '16 at 11:53
  • your value is not set in textview ? right ? @l2win36 – Amit Vaghela Jan 19 '16 at 12:09
  • yes, value is set in edittext and this value is not returned so @AmitVaghela – l2win36 Jan 19 '16 at 12:14
  • "In OnItemClick don't start again the activity" - right, but Without these lines of code in OnItemClick my listview is not clickable... @Chol – l2win36 Jan 19 '16 at 12:19
  • Yes but do not put this line: startActivity(intent), use setResult(1, intent) – Chol Jan 19 '16 at 13:09
  • For some reason it does not work properly, value of listview is not returned, and it looks like " a backbutton to first activity" @Chol – l2win36 Jan 19 '16 at 14:33
  • Put a log in the OnActivityResult to see if it is reached, then take a closer look to the extra, you are putting a serializable, but you are trying to retrieve a string – Chol Jan 19 '16 at 14:44

3 Answers3

3
Intent intent = new Intent(); 
        intent.putExtra("name", selectedResult); 
        setResult(1, intent);
0

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);
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • Yes, I guess this is the way! – zIronManBox Jan 19 '16 at 12:14
  • Can I try it to edittext and return the value there? @AmitVaghela – l2win36 Jan 19 '16 at 12:21
  • Unfortunately not,this value is not returned so. Should I write " String name= getIntent().getStringExtra("name") " in MainActivity in method "onActivityResult" ? @AmitVaghela – l2win36 Jan 19 '16 at 12:59
  • @AmitVaghela Maybe the problem is the announcement of the list in MainActivity, actually that's it - private ListView list1 = null; private String s1[] = {"a", "b", "c", "d", "e", "f"} – l2win36 Jan 19 '16 at 13:52
  • Thanks for your suggestions, but still its not working @AmitVaghela – l2win36 Jan 19 '16 at 15:51
  • I updated the post, still result in the list is not saved in editext. There is no error in the debugger. Can you take a look? @AmitVaghela – l2win36 Jan 19 '16 at 16:48
  • what you are getting in 'name' , String name = getIntent().getStringExtra("name"); @I2win36 – Amit Vaghela Jan 19 '16 at 17:20
  • @AmitVaghela I'm sorry I know why it get an error,now everything looks great, Thanks you – l2win36 Jan 19 '16 at 18:12
0

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();
});
Yev Kanivets
  • 1,780
  • 3
  • 22
  • 38