将Id传递给AlertDialog单击事件

时间:2022-08-26 10:00:58

I have a mapview with pointers, when a pointer is clicked I show an alertdialog. In this dialog I have a bit of text and two buttons, a positive and negative. When the positive is clicked I want to open a new activity based on the id of the pointer clicked.

我有一个带指针的mapview,当点击指针时我会显示一个alertdialog。在这个对话框中,我有一些文字和两个按钮,正面和负面。单击肯定时,我想根据单击的指针的id打开一个新活动。

I am new to Android and java and I am having trouble passing the ID to the click event.

我是Android和java的新手,我无法将ID传递给click事件。

My code so far..

我的代码到目前为止..

       List<myItemType> myItems= //code to get list of items
                final ArrayList<OverlayItem> items = new ArrayList<OverlayItem>(

    );

            for (myItemType item : myItems) {

            Double lat = Double.parseDouble(item .Lat);
            Double lng = Double.parseDouble(item .Long);

            items.add(new OverlayItem(item .ID, item .Name, item .Description
                    .substring(0, 20) + "...", new GeoPoint(lat, lng)));
        }
    ItemizedOverlay<OverlayItem> myOverLay = 
            new ItemizedOverlay<OverlayItem>(
                this, items, this.getResources().getDrawable(R.drawable.standard_pointer), new Point(5, 5),
                HotspotPlace.BOTTOM_CENTER, new ItemizedOverlay.OnItemGestureListener<OverlayItem>() 
                {
                    @Override
                    public boolean onItemSingleTapUp(final int index,final OverlayItem item) {

                        AlertDialog.Builder dialog = new AlertDialog.Builder(MapActivity.this);

                        dialog.setTitle(item.mTitle);
                        dialog.setMessage(item.mDescription);
                        dialog.setPositiveButton(R.string.View,new DialogInterface.OnClickListener() 
                        {

//********************CLICK EVENT HERE
                                    @Override
                                    public void onClick(DialogInterface dialog,int which) {
                                        Intent i = new Intent(MapActivity.this,POI.class);
                                        i.putExtra("Id", item.mKey); //<------ item.mKey is null!
                                        startActivity(i);
                                    }
                        });
                        dialog.setNegativeButton(R.string.Cancel, null);
                        dialog.create();
                        dialog.show();

                        return true;
                    }

The Id is stored in the "item.mKey", I know I can't access it directly, but I can't work out how to pass it in. Can anyone point me in the right direction?

Id存储在“item.mKey”中,我知道我无法直接访问它,但我无法弄清楚如何传入它。任何人都能指出我正确的方向吗?

Bex

2 个解决方案

#1


0  

assuming your mKey is a String, try this out. Write final String key = item.mkey; before dialog.setPositiveButton and then in the onClick method i.putExtra("Id", key);

假设你的mKey是一个字符串,试试这个。写最后的String key = item.mkey;在dialog.setPositiveButton之前,然后在onClick方法i.putExtra(“Id”,key);

#2


0  

Just to add an answer to how I did it.. I added a global variable to use rather than pass in the ID.

只是为我添加的答案添加一个答案..我添加了一个全局变量来使用而不是传入ID。

#1


0  

assuming your mKey is a String, try this out. Write final String key = item.mkey; before dialog.setPositiveButton and then in the onClick method i.putExtra("Id", key);

假设你的mKey是一个字符串,试试这个。写最后的String key = item.mkey;在dialog.setPositiveButton之前,然后在onClick方法i.putExtra(“Id”,key);

#2


0  

Just to add an answer to how I did it.. I added a global variable to use rather than pass in the ID.

只是为我添加的答案添加一个答案..我添加了一个全局变量来使用而不是传入ID。