简体   繁体   中英

Pass ListView values to multiple EditText in another Activity

I have an Activity that has two clickable EditTexts and I am trying to pass value from two different ListView Activities. Every time I try to populate value to the second EditText, the value of the other EditText is cleared.

I need help in figuring out where I am wrong and a possible resolution.

This is the code for the Activity with both EditTexts

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.book_seat);


        //Departure
        depart = (EditText)findViewById(R.id.departure_terminal);
        // This is a listener for the one way departure terminal
        depart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BookSeat.this,TerminalList.class);
                startActivity(intent);
            }
        });
        // Receiving value into activity using intent.
        String TempHolder = getIntent().getStringExtra("ListViewClickedValue");
        // Setting up received value into EditText.
        depart.setText(TempHolder);


        //Arrival
        arrive = (EditText)findViewById(R.id.arrival_terminal);
        // This is a listener for the one way arrival terminal
        arrive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BookSeat.this,ArrivalTerminalList.class);
                startActivity(intent);
            }
        });
        // Receiving value into activity using intent.
        String TempHolder_1 = getIntent().getStringExtra("ArrivalListViewClickedValue");
        // Setting up received value into EditText.
        arrive.setText(TempHolder_1);

This is the code for the first ListView

public class TerminalList extends AppCompatActivity {

    ListView listView;

    // Define string array.
    String[] listValue = new String[] {"Anambra => Awka","Delta(South-East) => Asaba(Onitsha)","Enugu => Enugu",
            "FCT Abuja => Kubwa","Imo => Owerri","Kaduna => Kaduna","Lagos => Cele/Okota","Lagos => Festac(MazaMaza)","Plateau => Jos"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.terminal_list);

        listView = (ListView)findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, listValue);

        listView.setAdapter(adapter);

        // ListView on item selected listener.
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub

                // Getting listview click value into String variable.
                String ListViewClickedValue = listValue[position].toString();

                Intent intent = new Intent(TerminalList.this, BookSeat.class);

                // Sending value to another activity using intent.
                intent.putExtra("ListViewClickedValue", ListViewClickedValue);

                startActivity(intent);
                finish();

            }
        });
    }
}

This is the code for the second ListView

public class ArrivalTerminalList extends AppCompatActivity {

    ListView listView;

    // Define string array.
    String[] listValue = new String[] {"Anambra => Awka","Delta(South-East) => Asaba(Onitsha)","Enugu => Enugu",
            "FCT Abuja => Kubwa","Imo => Owerri","Kaduna => Kaduna","Lagos => Cele/Okota","Lagos => Festac(MazaMaza)","Plateau => J-Town"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.arrival_terminal_list);

        listView = (ListView)findViewById(R.id.listView2);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, listValue);

        listView.setAdapter(adapter);

        // ListView on item selected listener.
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub

                // Getting listview click value into String variable.
                String ArrivalListViewClickedValue = listValue[position].toString();

                Intent intent = new Intent(ArrivalTerminalList.this, BookSeat.class);

                // Sending value to another activity using intent.
                intent.putExtra("ArrivalListViewClickedValue", ArrivalListViewClickedValue);

                startActivity(intent);

            }
        });
    }
}

When you start a new activity, your data from current activity intent won't be sent. You can either forward your data or you can start these activities for result. You can use startActivityForResult() to start your activities and receive result in the original activity. Instead of calling startActivity() you need to call startActivityForResult()

startActivityForResult(intent, ARRIVAL_REQUEST_CODE);    // ARRIVAL_REQUEST_CODE is arbitrary number >= 0

then in your arrival activity, when returning the result, you need to set the result that will be returned to BookSeat activity

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    ...

    Intent result = new Intent();
    result.putExtra("ArrivalListViewClickedValue", ArrivalListViewClickedValue);
    setResult(Activity.RESULT_OK, result);
    finish();
}

and finally to handle the result, you need to override function in BookSeat activity that will be called when you receive result from your ArrivalTerminalList activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
    if (requestCode == ARRIVAL_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            String arrivalValue = resultIntent.getStringExtra("ArrivalListViewClickedValue");
            arrive.setText(arrivalValue);    // You can make 'arrive' instance variable and initialize it in onCreate
        }
    }
}

You can do the same for departure activity. The advantage of this solution is that now your back stack will not be filling up because instead of starting new activity, you just return a result to previous activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM