簡體   English   中英

使用getextra將有意圖的xml整數數組傳遞給另一個活動

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

我將如何去做。 我能夠使用getExtra傳遞xml字符串數組,但是我不知道對包含一堆@drawable引用的整數數組執行語法。

這是我如何引用字符串數組

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

這就是我現在如何通過我的mainactivity.java中的字符串數組與putExtra一起使用它的方式

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);

在intent.putExtra(“ route”,routeDetail [i])中; startActivity(intent); }}}});

這是我的routeDetail.java中的getExtra

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);

首先將圖像資源存儲在Integer中

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

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

然后在路線Extra的下方添加此代碼

intent.putExtra("imageResourceId", imageId);

然后像這樣獲得Extra

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM