简体   繁体   中英

Pass data from listview to main activity

I have a main activity included the button and textview. When I click the button, it will display a listview and fill some data into that listview. In the listview, I click a item, I want to send the item data to textview in main activity. However, it has crash. I have no idea to find the problem and log (does not show). Could you look at my code and provide me the solution?

MainActivity

btnManage = (Button) findViewById(R.id.btnManage);
btnManage.setText(manageLabel);
btnManage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        Intent intent = new Intent(v.getContext(), ListViewActivity.class);
        intent.putExtra("List_data", "Hello");
        v.getContext().startActivity(intent);

    }
});

In ListViewActivity I have

Bundle bundle = getIntent().getExtras();
String data_String= bundle.getString("List_data");
ArrayList<String> data = new ArrayList<String>();
data.add(data_String);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, data);    
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter ); 
//Click one item
public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {

String  data_send    = (String) mainListView.getItemAtPosition(position);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Data_Send", data_send);
startActivity(intent);
 }

In the onCreate function in MainActivity, I have

Bundle bundle = getIntent().getExtras();
String data_activity =bundle.getString("Data_Send");

The Manifest file

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ListViewActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait"
    />

In ListViewActivity you are trying to start MainActivity which is already started hence the error.

You need to finish ListViewActivity acivity so it just goes back to the previous activity which is the MainActivity and pass data

in ListViewActivity you finish the activity and pass data back like this

Intent intent = new Intent();
intent.putExtra("Data_Send", data_send);
setResult(RESULT_OK, intent);
finish();

in MainActivity you start the ListViewActivity like this

Intent i = new Intent(getApplicationContext(), ListViewActivity.class);
i.putExtra("List_data", "Hello");
startActivityForResult(i, 1);

also in MainActivity you need to get data back from the ListViewActivity so you add

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String data = data.getStringExtra("Data_Send");

              // do whatever with data string
}}};

The if (requestCode == 1) { is the number id you give when you start an activity with startActivityForResult(i, 1); . If you want to do the same with another activity you can do startActivityForResult(i, 2); and to get back data you check for id 2 if (requestCode == 2) { ...

  1. after clicking and list item if you want to finish the list activity then you can do one thing. you can start the ListActivity using startActivityForResult . then when the list item is clicked then you can send result to the main activity.

to know more about it you can check the doc

  1. if you dont want to finish the List activity immediately after list item click then you can do some other things. a. you can send a broadcast to the main activity and receive the broadcast. b. as your mainactivity's text view will show only one item then you can store the item in a public variable

You should have a look at the following guide which outlines how to start an activity, and then wait for a result.

The problem with the way you are currently doing is that the "back stack" has 2 references to the main activity - ie hitting back will take you to the list activity, then to main activity again.

The advantage of using startActivityForResult() allows you to separate out the logic of creating the MainActivity , and handling the result of the ListViewActivity .

You can use startActivityForResult for this purpose. When you call Activity.startActivityForResult(), you set the requestCode. Later, this request code is needed by onActivityResult() in order to determine what Activity is sending data to it. We don't need to supply requestCode again on setResult() because the requestCode is carried along.

The data is intent data returned from launched intent. We usually use this data when we set extras on the called intent.

Consider this example:

CALL SECOND ACTIVITY

Intent i = new Intent(MainActivity.this, CheckActivity.class);
startActivityForResult(i, REQUEST_CODE_CHECK);

ON SECOND ACTIVITY, SET INTENT RESULT

getIntent().putExtra("TADA", "bla bla bla");
setResult(RESULT_OK, getIntent());
finish();

BACK TO FIRST ACTIVITY, ONACTIVITYRESULT()

if(requestCode == REQUEST_CODE_CHECK && resultCode == RESULT_OK){
    text1.setText(data.getExtras().getString("TADA") );
}

There you go. You should now understand what is Intent data and how to set and fetch the value.

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