简体   繁体   中英

Passing string in array from parent activity to child activity

This code simply has 2 buttons "Samsung" and "Apple" that start another activity listing a bunch of devices. How can I pass the respective company name (eg Samsung) to the child activity? Do I have to make the array global to do it? I only know how to pass the position

I know how to pass it in one function but I have two in this case, one setting up the custom list adapter, and the other is just an onClick function. And this uses arrays rather than standalone strings so I'm not sure how to deal with that

MainActivity.java

package org.turntotech.navigatesample;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends ListActivity implements OnItemClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("TurnToTech", "Project Name - NavigateSample");
        String[] data = { "Samsung", "Apple" };
        int[] icons = { R.drawable.samsung_logo, R.drawable.opo };

        // Provide the cursor for the list view. 
        setListAdapter(new CustomListAdapter(this, data, icons));

        /* setOnItemClickListener() Register a callback to be invoked when an item 
         * in this AdapterView has been clicked.
         */
        getListView().setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Intent intent = new Intent(parent.getContext(), ChildActivity.class);

        // Add extended data to the intent.
        intent.putExtra("POSITION", position);

        /*
         * Launch a new activity. You will not receive any information about when 
         * the activity exits. This implementation overrides the base version, 
         * providing information about the activity performing the launch.
         */
        startActivity(intent);
    }

}

ChildActivity.java

package org.turntotech.navigatesample;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;


public class ChildActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[][] data = {
                { "Galaxy Tab", "Galaxy Smart Phones", "Galaxy Gear" },
                { "iPhone", "iPad", "iPod" } };
        int[][] icons = {
                { R.drawable.gala, R.drawable.duos, R.drawable.star },
                { R.drawable.a, R.drawable.b, R.drawable.c }, };
        Intent intent = getIntent();
        int position = intent.getIntExtra("POSITION", 0);

        // Provide the cursor for the list view. 
        setListAdapter(new CustomListAdapter(this, data[position],
                icons[position]));

    }

}

You can just pass the String using putExtra(java.lang.String, java.lang.String) and retrieve it using getStringExtra(java.lang.String)

Or if the array is static you could declare it in XML and pass the position

Edit: Added Example

Example arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="brands">
        <item>Samsung</item>
        <item>Apple</item>
    </string-array>

    <string-array name="apple_models">
        <item>iPhone</item>
        <item>iPad</item>
        <item>iPod</item>
    </string-array>

    <string-array name="samsung_models">
        <item>Galaxy Tab</item>
        <item>Galaxy Smart Phones</item>
        <item>Galaxy Gear</item>
    </string-array>
</resources>

Then retrieve using:

Intent intent = getIntent();
int position = intent.getIntExtra("POSITION", 0);

String[] brandArray = getResources().getStringArray(R.array.brands);
String brandName = brandArray[position];

Example 1:

You can pass the session id to signout parent activity in the intent you're using to start the activity.

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)

Access the intent in child activity.

String str = getIntent().getStringExtra("EXTRA_SESSION_ID");

Example 2:

In your current Activity, create a new Intent:

Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("key","value");
startActivity(intent);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("key");
}

Use this technique to pass variables from one Activity to the other.

You don't really need to have a generic ChildActivity class. I think the easiest way to do it is to have two activities. SamsungChildActivity and AppleChildActivity. Both can inflate the same layout but the content will be the specific for one or another. Also you should put your resources in the resources folder like the array of data (look the string-array resource). In the MainActivity just see which activity you want to start and then create an intent for SamsungChildActivity or for AppleChildActivity.

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