开发者

Android hashmap button id

I´m making a list of items and 2 buttons for each item. Ex: Item1 On Off Item2 On Off

I'm using a hashmap and i have an xml-file with a textview and two buttons. My problem is that when i click a button i want to know which item it belongs to. I don't know how many items i will have so i reuse the same buttons so i want to set an id for each row.

My xml-file looks like this.

 <TextView android:id="@+id/DEVICE_CELL"
     android:layout_width="180dip"
 开发者_StackOverflow    android:layout_height="wrap_content"/>

<Button android:id="@+id/OFF_BUTTON"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_alignParentBottom="true"
android:onClick="turnOff"/>

And this is my hashmap and so on.

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();

map = new HashMap<String, String>();
            map.put("device", name);
            map.put("on", "ON");
            map.put("off", "OFF");


            mylist.add(map);
    SimpleAdapter dev = new SimpleAdapter(this, mylist, R.layout.list_item,
                    new String[] {"device", "on", "off"}, new int[] {R.id.DEVICE_CELL, R.id.ON_BUTTON, R.id.OFF_BUTTON});
        list.setAdapter(dev);


Every View in Android lets you set a "Tag". The tag is just a Java Object, so you could set the item at the tag on the two buttons so you can retrieve it in the OnClickListener.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜