OK, there's a good chance I'm missing something but

1) I don't see anyway to configure choiceMode for a CHeckedTxtView
iether programatically or via the layout file.

2) I most definitely DO need to call #setChecked in #bindView because
otherwise the CheckedTextViews remain checked even even after the
cursor for the ListAdapter has been changed (which is possible in this
scenario via user choice). If you have a single cursor feeding your
ListAdapter you'll never notice it, but as soon as you switch between
different lists of data (and have some memory of user selection on
that data) you have to be able to set the checked value on the
CheckedTextView based upon the row and rowModel.


On Aug 16, 6:22 am, Romain Guy <romain...@android.com> wrote:
> Actually you want to use CheckedTextView with choiceMode. That's what
> CheckedTextView is for. However, you should not be calling setChecked
> from bindView(), but let ListView handle it. The problem was that you
> were doing ListView's job a second time. You don't need listeners
> (click on onlistitem), calls to setChecked, etc.
>
> On Sun, Aug 15, 2010 at 4:15 AM, William Ferguson
>
>
>
> <william.ferguson...@gmail.com> wrote:
> > To those who may follow, if you have a list of CheckedTextView you
> > probably don't want the ListView to have any choice mode. Ie don't
> > set:
> >   listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
>
> > And then you will be able to handle the checked state in your
> > ListAdapter and wou will also not get #bindView fired a ridiculous
> > number of times.
>
> > On Aug 15, 5:26 pm, William Ferguson <william.ferguson...@gmail.com>
> > wrote:
> >> OK, further info.
>
> >> As could be seen from #newView, I was handling the onClick on the
> >> CheckedTextView myself and this was because I was using a normal
> >> Activity instead of a ListActivity.
> >> When I switched to a ListActivity, implemented
> >> ListActivity#onListItemClick and ditched the OnClickListener code
> >> above I noticed 2 things.
> >> 1) The CheckedText started staying checked even after scrolling (ie it
> >> starting to behave as expected).
> >> 2) Every time I toggle a list item, #bindView is being called once for
> >> each visible item.
>
> >> Its great that this has made it started working. But
> >> a) I don't understand why it wasn't working before
> >> b) 2 from above seems pretty wasteful. Why should #bindView be called
> >> from all visible items just because one item has been checked/
> >> unchecked?
>
> >> On Aug 15, 4:29 pm, William Ferguson <william.ferguson...@gmail.com>
> >> wrote:
>
> >> > Um, sorry, in attempting to simplify the example that I presented to
> >> > you I didn't manage a full text search and replace.
> >> > The text for #bindView should have read (and does in the running code)
>
> >> >     @Override
> >> >     public void bindView(View view, Context context, Cursor cursor) {
> >> >         final CheckedTextView checkedText = (CheckedTextView) view;
> >> >         final MyCursor myCursor = (MyCursor) cursor;
>
> >> >         final Long rowId = myCursor.getRowId();
>
> >> >         checkedText.setChecked(rowModel.isSelected(rowId));
> >> >         checkedText.setText(myCursor.getDescription());
> >> >         checkedText.setTag(rowId);
>
> >> >         Log.v(TAG, "rowId=" + rowId + " settingChecked=" +
> >> > rowModel.isSelected(rowId));
> >> >     }
>
> >> > On Aug 14, 11:15 pm, Paul Turchenko <paul.turche...@gmail.com> wrote:
>
> >> > > I believe the issue is that you're getting rowId from
> >> > > rawContactsCursor (bindView method) which stays on the same record.
> >> > > IMO you should get row id from cursor coming to you in bindView. This
> >> > > way you'll get rowId for the record you are currently binding.
> >> > > To be simple, you are binding to the wrong record.
>
> >> > > On Aug 14, 7:50 am, William Ferguson <william.ferguson...@gmail.com>
> >> > > wrote:
>
> >> > > > I have a ListView that just contains a CheckedTextView.
> >> > > > I have a very simple CursorAdapter that populates CheckedTextViews.
> >> > > > When I click on an item, I can see that I am responding to the 
> >> > > > correct
> >> > > > row, store the value in my model and the CheckedText gets checked.
>
> >> > > > However, when I scroll down and then back up again, while I see that
> >> > > > the model contains the correct value (in #bindView), calling
> >> > > > #setChecked on the CheckedTextView has no effect. Ie All items are
> >> > > > unchecked.
>
> >> > > > Its gotta be something simple, but I can't see it. Any ideas?
>
> >> > > > public final class SimpleAdapter extends CursorAdapter {
>
> >> > > >     private static final String TAG = "SimpleAdapter";
>
> >> > > >     private final LayoutInflater inflater;
> >> > > >     private final RowModel rowModel = new RowModel();
>
> >> > > >     public SimpleAdapter(Activity context, MyCursor cursor) {
> >> > > >         super(context, cursor, true);
> >> > > >         inflater = (LayoutInflater)
> >> > > > context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
> >> > > >     }
>
> >> > > >     @Override
> >> > > >     public View newView(Context context, Cursor cursor, ViewGroup
> >> > > > parent) {
> >> > > >         final CheckedTextView checkedText = (CheckedTextView)
> >> > > > inflater.inflate(android.R.layout.simple_list_item_multiple_choice,
> >> > > > parent, false);
>
> >> > > >         final MyCursor myCursor = (MyCursor) cursor;
> >> > > >         checkedText.setText(myCursor.getDescription());
>
> >> > > >         final Long rowId = rawContactsCursor.getRowId();
> >> > > >         checkedText.setChecked(rowModel.isSelected(rowId));
> >> > > >         Log.v(TAG, "rowId=" + rowId + " settingChecked=" +
> >> > > > rowModel.isSelected(rowId));
>
> >> > > >         checkedText.setTag(rowId);
>
> >> > > >         checkedText.setOnClickListener(new View.OnClickListener() {
> >> > > >             public void onClick(View view) { // Toggle whether the 
> >> > > > Row
> >> > > > is selected or not.
> >> > > >                 final Long id = (Long) view.getTag();
> >> > > >                 Log.v(TAG, "toggling rowId=" + rowId);
> >> > > >                 final boolean checked = rowModel.toggleSelected(id);
> >> > > >                 final CheckedTextView checkedTextView =
> >> > > > (CheckedTextView) view;
> >> > > >                 checkedTextView.setChecked(checked);
> >> > > >             }
> >> > > >         });
>
> >> > > >         return checkedText;
> >> > > >     }
>
> >> > > >     @Override
> >> > > >     public void bindView(View view, Context context, Cursor cursor) {
> >> > > >         final CheckedTextView checkedText = (CheckedTextView) view;
>
> >> > > >         final MyCursor myCursor = (MyCursor) cursor;
> >> > > >         checkedText.setText(myCursor.getDescription());
>
> >> > > >         final Long rowId = rawContactsCursor.getRowId();
> >> > > >         checkedText.setChecked(rowModel.isSelected(rowId));
> >> > > >         Log.v(TAG, "rowId=" + rowId + " settingChecked=" +
> >> > > > rowModel.isSelected(rowId));
>
> >> > > >         checkedText.setTag(rowId);
> >> > > >     }
>
> >> > > > }
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> 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 Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to