Saturday 15 September 2012

java - ListView setOnItemClickListener doesn't work on Switch widget -


I'm trying to capture click event for a list of switches

  ListView List = (list view) Find VVBIID (RIDMan_view); ArrayAdapter & LT; String & gt; LTRadapter = New array adapter & lt; String & gt; (This, R.layout.list_main_view_item, R.id.switch1, value); List.setAdapter (LTRadapter); list.setOnItemClickListener (New AdapterView.OnItemClickListener () {@Override public void onItemClick (AdapterView & LT ;? & gt; adapterView, views, int position, long id) {Log.w ( "MiniTasker", "w00t" Integer. ToString + (Pos));}}};   

I've added Android :. For ListView, but to no avail descendantFocusability = "blocksDescendants"

Here is activity_main.xml:

  & lt; LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: Leaut_heit = "Match_perrent" Android: padding lift = "@ daemon / activity_horizontal_margin" Android: paddingrect = "@ daemon / activity_horizontal_margin" Android: padding = "@ daemon / activity_world_margin" Android: paddingbottom = "@ daemon /activity_world_margin" tools: Reference = "main activity" Andr. Left: orientation = "vertical" & gt; & Lt; ListView Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" android: id = "@ + id / main_view" Android: layout_gravity = "left | center_vertical" / & gt; & Lt; / LinearLayout & gt;   

And here is list_main_view_item.xml

  & lt; RelativeLayout xmlns: android = "Http://schemaskandroidkcom/apk/res/android" android: orientation = "horizontal" android: Layout_width = "Match_parent" Android: Layout_height = "Match_parent" Android: DescendantFocusability = "BlocksDescendants" Android: minHeight = "android: attr / listPreferredItemHeight" android: focusable = "false" Android: focusableInTouchMode = "false" & gt; & Lt; Android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "new switch" android: id = "@ + id / switch1" Android: layout_gravity = "center_horizontal | top" Android: layout_alignParentTop = "True" Android: layout_alignParentRight = "true" android: layout_alignParentLeft = "true" android: checked = "true" android: tag = "0" /> & Lt; / RelativeLayout & gt; OK   

OK You should add an ID to your list view in your XML.

Then you can apply that ID to your ListView in your method:

  ListView list = (ListView) findViewById (R.id.my_list_view);   

The reference to your list is currently pointing to your linear layout (white space outside your actual list view). This is why it detects clicks outside ListView

No comments:

Post a Comment