Thanks brothers --
I like the idea of the selected state of the data being in the
adapter, however, I'm not too excited about all the custom code for
such an obvious requirement! I am using a ListActivity subclass
configured as follows:
setListAdapter(buildNameAdapter());
ListView list = getListView();
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(this);
preCheck(list);
The adapter is a SimpleCursorAdapter instantiated as follows
private ListAdapter buildNameAdapter() {
String[] PROJECTION = new String[] { Contacts.People._ID,
Contacts.PeopleColumns.NAME };
Cursor c = managedQuery(Contacts.People.CONTENT_URI, PROJECTION,
null,
null, Contacts.People.DEFAULT_SORT_ORDER);
startManagingCursor(c);
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice, c,
new String[] { Contacts.PeopleColumns.NAME },
new int[] { android.R.id.text1 });
return adapter;
}
Where I stuck the adapter in the list and thought life would be good.
Now I am sitting here with my cursor blinking in the preCheck(...)
method wondering what the hell to do to have an entry(s) pre-checked!
It appears I can either loop through the adapter/list and manipulate
the list view (as per Mark) or muck around with the Cursor in order to
add a SelectableString custom type to the adapter *and* override
callback methods to check the entries which have their
(selectableString.isSelected()==true values. The basic idea of
wanting a list of items either checked/unchecked seems like one where
none of this hand-coding should be necessary. This all started when I
was looking for a way to add *one* extra checkbox to the basic contact
info! Now I'm writing myself a small mountain of code because
associating a single "extra" element with a contact is harder than
pulling teeth.
Does it sound like I am disappointed? Didn't we put a man on the
moon?
P.S. Thanks for the help you guys. Rolling up my sleeves now and
cracking open a cold brew. Perhaps I'll flip a coin on this one.
Peace,
Scott
On Oct 27, 10:57 am, Streets Of Boston <[email protected]>
wrote:
> 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 <[email protected]> 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$cursorwrapperin...@4375e088
> > list.getRootView() which returned
> > com.android.internal.policy.impl.phonewindow$decorv...@4375ac38
>
> > Can you throw me another clue?
>
> > Scott
>
> > On Oct 25, 4:02 pm, "Mark Murphy" <[email protected]> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---