简体   繁体   中英

using getextra to pass xml integer array with an intent to another activity

How would I go about doing this. I was able to pass a xml string array with getExtra but I dont know the syntax to do it for an integer array containg a bunch of @drawable references.

heres how im referencing the string array

 //fills route detail image view with xml array of images
final TypedArray image =  getResources().obtainTypedArray(R.array.routeImage);

heres how iI have it now with my putExtra using a string array in my mainactivity.java

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

                String route = values[position];

                //for loop to increment through each route list item
                int i;
                for (i=0; i < values.length;i++)
                {
                    if (route.equals(values[i]))
                    {
                        Intent intent = new Intent(view.getContext(), RouteDetails.class);

and heres in intent.putExtra("route", routeDetail[i]); startActivity(intent); } } } } );

and heres my getExtra in my routeDetail.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);

//TextView for route details
final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
routeDetailsView.setText(getIntent().getExtras().getString("route"));

//ImageView for route details
routeImage = (ImageView) findViewById(R.id.routeImage);
//routeImage.setImageResource(R.drawable.birdsboroareamap);

First store the image Resource in Integer

final TypedArray image =  getResources().obtainTypedArray(R.array.routeImage);

int imageId = (int) image.getResourceId(i, -1);

Then add this code right below putting the route Extra

intent.putExtra("imageResourceId", imageId);

Then get the Extra Like this

routeImage.setImageResource(getIntent.getIntExtra("imageResourceId", 0); 

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