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, 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to