Tuesday, October 27, 2009

[android-developers] Re: ListView setAdapter in onCreate, getChildCount is zero in onStart?

Put the data whether a checkbox of a list-item is selected inside the
*adapter's* data, like my example i gave you in an earlier post
(selectableString.isSelected).

If you want to pre-select a check-box of a list-item, set its
corresponding element in the adapter (SelectableString) as selected
(selectableString.isSelected = true). Then, if you want to show this
on the screen, call notifyDatasetChanged() on your adatper.

If you implemented your adapter's getView correctly, you'd see the
checkbox becoming checked.
Example code (won't compile):

public MyAdapter extends BaseAdapter {
private List<SelectableString> mData = new
ArrayList<SelectableString>();
...

// example methods.
public void selectSoundTableItem(int position, boolean select) {
mData.get(position).isSelected = select;
notifyDatasetChanged();
}

public void setSoundData(Map<String,Integer> soundTable) {
mData.clear();
for (String soundName : soundTable.keySet()) {
SelectableString ss = new SelectableString();
ss.soundTableName = soundName;
ss.soundId = soundTable.get(soundName);
mData.add(ss);
}
notifyDatasetChanged();
}

public void preselectSoundData(SharedPreferences prefs) {
String[] selectedSoundsArray =
prefs.getString("selectedSounds", "").split(",");

for (SelectableString ss : mData) {
ss.isSelected = false;
}

for (int selI = 0; selI < selectedSoundsArray.length; selI++) {
SelectableString ss = findItem(mData,selectedSoundsArray
[selI]);
ss.isSelected = true;
}
notifyDatasetChanged();
}

// below are the methods from BaseAdapter you need to implement:
// getCount(), getItem(int position), etc.
...
public Object getItem(int position) {
return mData.get(position);
}
...
...

// Called by your attached ListView when it's about to create/re-
use a visible item.
// The ListView calls this method. Your code should not call it.
public View getView(int position, View convertView, ViewGroup
parent) {
... // use convertView or create/inflate a new one when it's
null. //
...

SelectableString ss = (SelectableString)getItem(position);

// may help you find back the soundTableItem in onClick methods
by calling 'getTag()'
convertView.setTag(new Integer(ss.soundId));

String soundName = ss.soundName;
TextView tv = (TextView)convertView.findViewById(R.id.textview);
tv.setText(soundName);

boolean isSelected = ss.isSelected;
CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);
cb.setChecked(isSelected);

...
...

return convertView;
}
...
}


On Oct 26, 9:38 pm, stanlick <stanl...@gmail.com> wrote:
> Thanks Mark --
>
> Doesn't this sort of violate the MVC pattern?  Moreover, does it seem
> odd to you that both the adapter and ListView have methods to cough up
> a view?  Pre-selecting check boxes is sure becoming a difficult-to-do
> process!
>
> I am in the debugger and experimenting with how I might get the view
> from the list as you suggested.
>
> I have tried
>
> list.getChildAt(0); which returned null
> list.getItemAtPosition(0) which returned
> android.content.ContentResolver$CursorWrapperInner@4375e088
> list.getRootView() which returned
> com.android.internal.policy.impl.PhoneWindow$DecorView@4375ac38
>
> Can you throw me another clue?
>
> Scott
>
> On Oct 25, 4:02 pm, "Mark Murphy" <mmur...@commonsware.com> wrote:
>
>
>
> > > I am using a SimpleCursorAdapter and having a similar problem with pre-
> > > checking certain entries.  This is my experimental code that I am
> > > trying immediately following the instantiation of the adapter:
>
> > >            adapter = new SimpleCursorAdapter(this,
> > >                            android.R.layout.simple_list_item_multiple_choice, c,
> > >                            new String[] { Contacts.PeopleColumns.NAME },
> > >                            new int[] { android.R.id.text1 });
>
> > >                 // TODO TEST CODE
> > >            CheckedTextView cb = (CheckedTextView) adapter.getView(0, null,
> > > null);
> > >            cb.setChecked(true);
>
> > > The list appears and the first entry is *not* checked.  Any ideas?
>
> > The View you got from your getView() call is being discarded.
>
> > Remember, *ListView* does the caching of rows. Since your requested a row
> > outside of ListView, ListView doesn't know about it or use it.
>
> > Add your SimpleCursorAdapter to your ListView, then call setItemChecked()
> > on the ListView to check your desired row, and see if that works.
>
> > --
> > Mark Murphy (a Commons Guy)http://commonsware.com
> > Android App Developer Books:http://commonsware.com/books.html- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments:

Post a Comment