#setChecked in CheckedTextView in ListView not working

by William Ferguson » Sat, 14 Aug 2010 12:50:52 GMT


Sponsored Links
 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);
    }
}

--



#setChecked in CheckedTextView in ListView not working

by Paul Turchenko » Sat, 14 Aug 2010 21:15:28 GMT


 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:50am, William Ferguson <william.ferguson...@gmail.com>



--


Sponsored Links


#setChecked in CheckedTextView in ListView not working

by William Ferguson » Sun, 15 Aug 2010 14:29:28 GMT


 m, 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:15pm, Paul Turchenko <paul.turche...@gmail.com> wrote:

--



#setChecked in CheckedTextView in ListView not working

by William Ferguson » Sun, 15 Aug 2010 15:26:20 GMT


 K, 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:29pm, William Ferguson <william.ferguson...@gmail.com>
wrote:

--



#setChecked in CheckedTextView in ListView not working

by William Ferguson » Sun, 15 Aug 2010 19:15:22 GMT


 o 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:26pm, William Ferguson <william.ferguson...@gmail.com>
wrote:

--



#setChecked in CheckedTextView in ListView not working

by Romain Guy » Mon, 16 Aug 2010 04:22:40 GMT


 ctually 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:
>



#setChecked in CheckedTextView in ListView not working

by William Ferguson » Mon, 16 Aug 2010 11:30:51 GMT


 K, 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:22am, Romain Guy <romain...@android.com> wrote:
> >



#setChecked in CheckedTextView in ListView not working

by Romain Guy » Mon, 16 Aug 2010 11:36:59 GMT


 gt; 1) I don't see anyway to configure choiceMode for a CHeckedTxtView

choiceMode is set on ListView, not on the CheckedTextView. For
instance:
http://d.android.com/reference/android/widget/ListView.html#setItemChecked(int,
boolean)


Use ListView's various choiceMode related methods to do this. You can

>> >



#setChecked in CheckedTextView in ListView not working

by William Ferguson » Mon, 16 Aug 2010 13:38:44 GMT


 K. We seem to be going in circles here.

If I set choiceMode on the ListView (which I was doing originally),
then you are correct in that I don'I need to setChecked on the CTV.
And in fact it just plain won't work as I said previously. There is an
Android issue that clued me into this.

However, it also means that whether an item is checked or not is held
within some internal mechanism of the ListView. This is fine (at least
you never notice that its not) as long as you never change the cursor
that is backing the CursorAdapter. If you do change the cursor using
CursorAdapater#changeCursor, then even though a new set of items get
displayed, they will be checked or not checked based upon what was
selected from the old cursor. The only way that I could see to get
ListView#choiceMode to work in this scenario would be (on change of
cursor) to call ListView#clearChoices and then iterate over all the
items in my cursor calling ListView#setItemChecked.

And since a cursor can be arbitrarily long this seemed like a really
daft way to do it. It was also a lot less clear and more code

But if I'm missing something, I'm all ears.


On Aug 16, 1:36pm, Romain Guy <romain...@android.com> wrote:
> >> >



Other Threads

1. SIGSEGV error during playback with externally linked library

Hi All,

Am currently integrating a new parser into Android 2.1. The parser is
an archive file compiled using a different toolchain i.e. not the
toolchain present in the android solution.
There is no linking error during integration.
But am getting the following error during runtime:

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::AddCommandToQueue()
Type=13 ID=2 APIcmd=1 Tick=170

E/PV      ( 1907): PVLOG:TID(0x47258):Time=170:PVPlayerEngine::Run()
In

E/PV      ( 1907): PVLOG:TID(0x47258):Time=170:PVPlayerEngine::Run()
Processing command with type=13

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::DoInit() Tick=170

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::DoInit() In

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::DoSourceNodeInit()
Tick=170

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::DoSourceNodeInit() In

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVMFXXXFFParserNode::Init() called

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVMFXXXFFParserNode::QueueCommandL()
called id=1009

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::DoSourceNodeInit() Out

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::DoInit() Out

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVPlayerEngine::SetEngineState() In
Current state 1, New state 2

E/PV      ( 1907): PVLOG:TID(0x47258):Time=170:PVPlayerEngine::Run()
Out

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVMFXXXFFParserNode::DoInitNode() In

E/PV      ( 1907):
PVLOG:TID(0x47258):Time=170:PVMFXXXFFParserNode::ParseXXXFile() In

I/ActivityManager( 1095): Displayed activity
aricent.android.centdroid/.playMedia: 412 ms (total 412 ms)

I/DEBUG   ( 1031): *** *** *** *** *** *** *** *** *** *** *** *** ***
*** *** ***

I/DEBUG   ( 1031): Build fingerprint: 'Samsung/halo/halo/zoom2:2.1-
update1/ERE27/eng.neoio.lee.20100331.110710:eng/test-keys'

I/DEBUG   ( 1031): pid: 1907, tid: 2069  >>> /system/bin/mediaserver
<<<

I/DEBUG   ( 1031): signal 11 (SIGSEGV), fault addr 4050900c

I/DEBUG   ( 1031):  r0 40608bbc  r1 00000000  r2 00000000  r3 00000000

I/DEBUG   ( 1031):  r4 a712bb60  r5 40608bbc  r6 00000000  r7 00000000

I/DEBUG   ( 1031):  r8 40608bbc  r9 000a0444  10 40509000  fp 00047888

I/DEBUG   ( 1031):  ip 00000000  sp 40608a78  lr a70e7ca4  pc
a70e98bc  cpsr 60000010

I/DEBUG   ( 1031):          #00  pc 000e98bc  /system/lib/
libopencore_player.so

I/DEBUG   ( 1031):          #01  lr a70e7ca4  /system/lib/
libopencore_player.so

I/DEBUG   ( 1031):

I/DEBUG   ( 1031): code around pc:

I/DEBUG   ( 1031): a70e98ac e3560000 0a000001 e1a00006 ebfffb0e

I/DEBUG   ( 1031): a70e98bc e59a000c ebfffb0f e3a00000 e28dd01c

I/DEBUG   ( 1031): a70e98cc e8bd8ff0 e59d3014 e2800024 e58d3004

I/DEBUG   ( 1031):

I/DEBUG   ( 1031): code around lr:

I/DEBUG   ( 1031): a70e7c94 e1a00005 e1a02004 e59f1468 eb0006cf

I/DEBUG   ( 1031): a70e7ca4 e3500000 e5880000 0a000065 e3a03000

I/DEBUG   ( 1031): a70e7cb4 e5885004 e5c83150 e2881018 eb0004ee

I/DEBUG   ( 1031):

I/DEBUG   ( 1031): stack:

I/DEBUG   ( 1031):     40608a38  00000000

I/DEBUG   ( 1031):     40608a3c  00000000

I/DEBUG   ( 1031):     40608a40  0000003f

I/DEBUG   ( 1031):     40608a44  00e14d17

I/DEBUG   ( 1031):     40608a48  ffff0208

I/DEBUG   ( 1031):     40608a4c  40608b27

I/DEBUG   ( 1031):     40608a50  40608c60

I/DEBUG   ( 1031):     40608a54  7ffffffc

I/DEBUG   ( 1031):     40608a58  afe3dba4

I/DEBUG   ( 1031):     40608a5c  afe0f110  /system/lib/libc.so

I/DEBUG   ( 1031):     40608a60  afe3db74

I/DEBUG   ( 1031):     40608a64  afe0f110  /system/lib/libc.so

I/DEBUG   ( 1031):     40608a68  00000000

I/DEBUG   ( 1031):     40608a6c  afe0f028  /system/lib/libc.so

I/DEBUG   ( 1031):     40608a70  df002777

I/DEBUG   ( 1031):     40608a74  e3a070ad

I/DEBUG   ( 1031): #00 40608a78  00089730  [heap]

I/DEBUG   ( 1031):     40608a7c  afe3dea4

I/DEBUG   ( 1031):     40608a80  00100000

I/DEBUG   ( 1031):     40608a84  afe0bc07  /system/lib/libc.so

I/DEBUG   ( 1031):     40608a88  00000000

I/DEBUG   ( 1031):     40608a8c  00000000

I/DEBUG   ( 1031):     40608a90  afe44c7c

I/DEBUG   ( 1031):     40608a94  00000000

I/DEBUG   ( 1031):     40608a98  40608bbc

I/DEBUG   ( 1031):     40608a9c  00000000

I/DEBUG   ( 1031):     40608aa0  afe3dea4

I/DEBUG   ( 1031):     40608aa4  00089730  [heap]

I/DEBUG   ( 1031):     40608aa8  000a0444  [heap]

I/DEBUG   ( 1031):     40608aac  40509000

I/DEBUG   ( 1031):     40608ab0  00047888  [heap]

I/DEBUG   ( 1031):     40608ab4  a70e7ca4  /system/lib/
libopencore_player.so

I/DEBUG   ( 1031):     40608ab8  00000000

I/DEBUG   ( 1031):     40608abc  9d98cd25

W/AudioSystem( 1095): AudioFlinger server died!

W/MediaPlayer( 1095): MediaPlayer server died!

E/MediaPlayer( 1095): error (100, 0)

The function PVMFXXXFFParserNode::ParseXXXFile() calls an API of the
archive file and the code is crashing in that archive file.

The same parser works fine when integrated with Android donut release.

Is the error related to the wrong toolchain or is there something that
I am missing?

Regards
Venu

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Android-x86

Bros,

Ane mau cobain android-x86 buat di virtualbox.. Iseng2 aja pgn tau.. Tp cuma 
ada yg versi 1,6... Kira2 ada yg tau gk yg versi eclairnya? Ato cr bikin image 
sendiri untuk eclair?...

Thanks ya bro..

Fery,

-- 
"Indonesian Android Community [id-android]" 

3. Need Help on Quick Search

4. Hancent SMS & Default App SM

5. VIBRATE permission not showing on device or emulator

6. Voip Rakyat

7. Live Wallpaper Error