[android-beginners] Re: Unable to check correct row when using CheckedTextView
As Romain said, top-level View has to be Checkable. A solution could be to make a custom container implementing Checkable interface, then use that container as top-level View for your row. Overriden methods just call analog methods in Checkable item inside the container. Example: public class CheckedLayout extends LinearLayout implements Checkable { private CheckedTextView checkbox; private ImageView image; private TextView text; private Context context; ... @Override public boolean isChecked() { return checkbox.isChecked(); } @Override public void setChecked(boolean checked) { checkbox.setChecked(checked); } @Override public void toggle() { checkbox.toggle(); } } -- 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
[android-beginners] Re: Unable to check correct row when using CheckedTextView
OK. I am an idiot. This was not working for me because I had left-over code in onCreate from something else I tried: //listView.setItemsCanFocus(false); //listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); In the process I learned an awful lot from Mark Murphy's examples in Fancy Lists section of the Busy Coders Guide ( http://commonsware.com/Android/index.html) and Romain Guy's YouTube presentation here (http://www.youtube.com/watch?v=UApv-ZMJ51g). Jason --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-beginners] Re: Unable to check correct row when using CheckedTextView
Does anyone know if this thread ended well? I attempting to create a fancy list with a multiple selection (in 1.6). The following code give seemingly random results using a SimpleCursorAdapter ... @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); CheckedTextView cb = (CheckedTextView) v.findViewById(R.id.bulkload_ok); Log.d(Peeps.TAG, "view clicked was .. " + cb.isChecked()); if (cb.isChecked()){ cb.setChecked(false); Log.d(Peeps.TAG, "set to .. false == " + cb.isChecked()); } else { cb.setChecked(true); Log.d(Peeps.TAG, "set to .. true == " + cb.isChecked()); } v.refreshDrawableState(); //select_track(); } LAYOUT HERE: http://schemas.android.com/apk/res/android"; android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="6sp"> --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-beginners] Re: Unable to check correct row when using CheckedTextView
> @Romain- > Thanks for responding. setChoiceMode is set to Mutiple choice. But it > doesnt check the row. Thats why I am manually trying it. That's because the top-level View in your item has to be Checkable. It's a current limitation of ListView :( > > On Feb 16, 4:12 am, Mark Murphy 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 >> > -- >> > >> > > > 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" >> >> > > > android:id="@+id/title" >> > android:layout_width="fill_parent" >> > android:layout_height="wrap_content" >> > android:textAppearance="?android:attr/textAppearanceLarge" >> > android:checkMark="?android:attr/listChoiceIndicatorMultiple" >> > /> >> > > > android:id="@+id/desc" >> > android:layout_width="fill_parent" >> > android:layout_height="wrap_content" >> > android:layout_below="@+id/title" >> > android:textAppearance="?android:attr/textAppearanceSmall" >> > /> >> > >> >> > xx >> > 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 > > > -- Romain Guy Android framework engineer romain...@android.com Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-beginners] Re: Unable to check correct row when using CheckedTextView
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 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 > > -- > > > > > 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" > > > > android:id="@+id/title" > > android:layout_width="fill_parent" > > android:layout_height="wrap_content" > > android:textAppearance="?android:attr/textAppearanceLarge" > > android:checkMark="?android:attr/listChoiceIndicatorMultiple" > > /> > > > android:id="@+id/desc" > > android:layout_width="fill_parent" > > android:layout_height="wrap_content" > > android:layout_below="@+id/title" > > android:textAppearance="?android:attr/textAppearanceSmall" > > /> > > > > > xx > > 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 -~--~~~~--~~--~--~---
[android-beginners] Re: Unable to check correct row when using CheckedTextView
Do not handle the checked state yourself. ListView recyles the view which can cause trouble. Lookup setChoiceMode() instead and refer to the ApiDemos for examples. On Sun, Feb 15, 2009 at 5:42 AM, 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 > -- > > 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" > > > android:id="@+id/title" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:textAppearance="?android:attr/textAppearanceLarge" > android:checkMark="?android:attr/listChoiceIndicatorMultiple" > /> > android:id="@+id/desc" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:layout_below="@+id/title" > android:textAppearance="?android:attr/textAppearanceSmall" > /> > > > xx > 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(); > } > > > > -- Romain Guy Android framework engineer romain...@android.com Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[android-beginners] Re: Unable to check correct row when using CheckedTextView
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 > -- > > 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" > > >android:id="@+id/title" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:textAppearance="?android:attr/textAppearanceLarge" > android:checkMark="?android:attr/listChoiceIndicatorMultiple" > /> >android:id="@+id/desc" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:layout_below="@+id/title" > android:textAppearance="?android:attr/textAppearanceSmall" > /> > > > xx > 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, 2009 http://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 -~--~~~~--~~--~--~---