Hi Mark, Thanks for the suggestion. I did try casting before and I've tried it again. It gives me a 'InvocationTargetException' when I cast it. When I put a debug watch for 'v' (the second parameter to onListItemClick ()), i see that 'v' is of type 'LinearLayout'. Under 'v', I also see a View[] and the first element in the array is a CheckedTextView and the second element - a TextView. So I tried this code:
LinearLayout layout = (LinearLayout)v; View childView = layout.getChildAt(0); if(childView != null && childView instanceof CheckedTextView) { CheckedTextView cv = (CheckedTextView)childView; cv.setChecked(!cv.isChecked()); } But it takes me back to where I started :o) Wrong row gets checked. I have 5 rows. Here is how the listview behaves: If I click on row1, row5 checkbox gets checked. Click on row1 again, row1 checkbox gets checked Click on row2, row4 checkbox gets checked. Click on row2 again, row2 checkbox gets checked BUT..when I click on row3, row3 gets checked correctly! Even if I have only 3 items the second item (middle one) gets checked correctly. Something to do with the layout...maybe?? @Romain- Thanks for responding. setChoiceMode is set to Mutiple choice. But it doesnt check the row. Thats why I am manually trying it. On Feb 16, 4:12 am, Mark Murphy <mmur...@commonsware.com> wrote: > Shams wrote: > > Hello, > > I have a custom multi-select listview with two lines per row and a > > checkbox in the first line. When I click on the checkbox, the checbox > > does not get checked. But the list items get checked/ unchecked. I > > want to check the checkbox corresponding to the row I clicked. If I > > use findViewById() in onListItemClick(), it does not find the correct > > checkbox. I click on a click and some other row gets checked. How can > > I get to check the correct row. Below is my main.xml and > > onListItemClick() code: > > > Main.xml > > -------------- > > <?xml version="1.0" encoding="utf-8"?> > > <LinearLayout > > xmlns:android="http://schemas.android.com/apk/res/android" > > android:layout_width="fill_parent" > > android:layout_height="wrap_content" > > android:orientation="vertical" > > android:paddingTop="5dip" > > android:paddingBottom="5dip" > > android:paddingLeft="5dip" > > > <CheckedTextView > > android:id="@+id/title" > > android:layout_width="fill_parent" > > android:layout_height="wrap_content" > > android:textAppearance="?android:attr/textAppearanceLarge" > > android:checkMark="?android:attr/listChoiceIndicatorMultiple" > > /> > > <TextView > > android:id="@+id/desc" > > android:layout_width="fill_parent" > > android:layout_height="wrap_content" > > android:layout_below="@+id/title" > > android:textAppearance="?android:attr/textAppearanceSmall" > > /> > > </LinearLayout> > > > xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx > > protected void onListItemClick(ListView l, View v, int position, long > > id) { > > super.onListItemClick(l, v, position, id); > > CheckedTextView cbox = (CheckedTextView)v.findViewById(R.id.title); > > if(cbox.isChecked()) > > cbox.setChecked(false); > > else > > cbox.setChecked(true); > > > v.refreshDrawableState(); > > } > > Don't use findViewById() to attempt to find something in rows in a > ListView. After all, if you have 20 visible rows, you will have 20+ > views named R.id.title. > > If the row you want to check is the one clicked upon, your > CheckedTextView should be the View supplied in the second parameter to > onListItemClick(). Just cast it to a CheckedTextView. > > -- > Mark Murphy (a Commons Guy)http://commonsware.com > > Android Training on the Ranch! -- Mar 16-20, > 2009http://www.bignerdranch.com/schedule.shtml --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---