Thank you for your response, and I can understand your confusion.
Yes, when I run this activity, my screen displays the title text, two
items in my ListView (0 = "Big Sound", 1 = "Little Sound"), and the
footer text.  I can select either or both items, and when I press
Back, I can step through my onPause (you are correct here, pardon my
bad form) and watch the selection indices get saved to
"selectedSounds" preference.

>From what I've observed through careful debugging, I'm not having any
issues saving the selections.  Nor am I having issues retrieving these
selections from preferences, or setting the ArrayAdapter to my
ListView.  The breakdown occurs when I try to set these items as
checked in the ListView when the activity starts back up.

> Don't use 'getChildCount' and 'getChildAt' method to check your
> preference-settings.

What is the proper method?  I think this is the crux of my question.

Again, I appreciate the help.  I think some further guidance is
needed.




On Oct 7, 3:30 pm, Streets Of Boston <[email protected]> wrote:
> Your activity (and your list-view) is not yet visible in the onStart.
>
> Don't use 'getChildCount' and 'getChildAt' method to check your
> preference-settings.
>
> BTW: If your activity is not yet visible, how can a user make a
> selection?
> Shouldn't you query in the onPause, for example.
> And also, the positions returned by the method getCheckedItemPositions
> () are the positions in your adapter (ArrayAdapter in your case). Your
> list-*view* positions (used in getChildAt) are not the same. You list-
> view will never have more children than the ones you can see. And the
> child-at postion 0 is the one on the top of the list-view.
>
> On Oct 7, 2:37 pm, KhanAzul <[email protected]> wrote:
>
> > I want to display a ListView with checkboxes, and I want some of the
> > checkboxes pre-selected.  The ListView has setAdapter called during
> > onCreate, and the list information appears when the screen is drawn.
> > However, when I attempt to set certain list items as checked, my
> > ListView reports getChildCount() = 0.
>
> > My SharedPreferences contains a list of sounds that have been
> > previously selected, as a comma delimited string of list indices.
> > Because the list appears to have no children in onStart, my attempts
> > to set items as checked fails.
>
> > On the good side, once the list is rendered, I can select entries and
> > hit Back, and my selection is stored to SharedPreferences correctly.
>
> > How can I load my previous selection into the list?  Should I be using
> > something else here?
>
> > In the spirit of open source, here's my code, free to you :)
>
> > ====================================================
> > SoundLibrary.Java
> > ====================================================
>
> > import java.util.Hashtable;
>
> > import android.app.Activity;
> > import android.content.Context;
> > import android.content.SharedPreferences;
> > import android.content.res.Resources;
> > import android.media.MediaPlayer;
> > import android.os.Bundle;
> > import android.util.SparseBooleanArray;
> > import android.view.View;
> > import android.view.View.OnLongClickListener;
> > import android.widget.ArrayAdapter;
> > import android.widget.ListView;
>
> > public class SoundLibrary extends Activity{
> >         ListView lstSounds;
> >         Hashtable<String, Integer> soundTable;
> >         CharSequence[] soundNames;
> >         CharSequence[] soundLocations;
> >         OnLongClickListener previewListener;
> >         static Context myContext;
> >         static MediaPlayer player;
> >         SharedPreferences prefs;
>
> >         @Override
> >     public void onCreate(Bundle savedInstanceState) {
> >         super.onCreate(savedInstanceState);
> >         setContentView(R.layout.soundlibrary);
> >         myContext = this.getApplicationContext();
> >         prefs = getSharedPreferences("myAppData", 0);
>
> >         soundTable = new Hashtable<String, Integer>();
> >         LoadSoundTable();
> >         lstSounds = (ListView)findViewById(R.id.lstSounds);
> >         PopulateSoundList();
> >         lstSounds.setOnLongClickListener(new PreviewListener());
> >         }
>
> >         @Override
> >         public void onStart() {
> >                 super.onStart();
> >                 String[] selectedSoundsArray =
> >                         prefs.getString("selectedSounds", "").split(",");
> >                 for (int selI = 0; selI < selectedSoundsArray.length; 
> > selI++) {
> >                         if (lstSounds.getChildCount() <= selI) {
> >                                 break;
> >                         }
> >                         try {
> >                                 
> > lstSounds.getChildAt(Integer.parseInt(selectedSoundsArray
> > [selI])).setSelected(true);
> >                         } catch (NumberFormatException e) { }
> >                 }
> >         }
>
> >         @Override
> >         public void onPause() {
> >                 super.onPause();
> >                 StringBuffer sb = new StringBuffer();
> >                 SparseBooleanArray sbaSelectedSounds =
> > lstSounds.getCheckedItemPositions();
> >                 for (int sbaKey = 0; sbaKey < sbaSelectedSounds.size(); 
> > sbaKey++)
> >                 {
> >                         if (sbaSelectedSounds.get(sbaKey, false))
> >                         {
> >                                 sb.append(String.valueOf(sbaKey));
> >                                 sb.append(',');
> >                         }
> >                 }
> >                 prefs.edit().putString("selectedSounds", 
> > sb.toString()).commit();
> >         }
>
> >         private void LoadSoundTable()
> >         {
> >                 Resources res = getResources();
> >                 CharSequence[] soundNames =
> >                         res.getTextArray(R.array.sound_name);
> >                 CharSequence[] soundLocations =
> >                         res.getTextArray(R.array.sound_location);
>
> >                 for (int i = 0; i < soundNames.length; i++) {
> >                         soundTable.put(soundNames[i].toString(),
> >                                         
> > res.getIdentifier(soundLocations[i].toString(), "raw",
> > this.getPackageName()));
> >                 }
> >         }
>
> >         private void PopulateSoundList()
> >         {
> >         ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
> >                         android.R.layout.simple_list_item_multiple_choice,
> >                         soundTable.keySet().toArray(new 
> > String[soundTable.keySet
> > ().size()]));
> >         lstSounds.setAdapter(aa);
> >         }
>
> >         private class PreviewListener extends Activity implements
> > OnLongClickListener {
> >                 public boolean onLongClick(View v) {
> >                         String previewSoundName = 
> > lstSounds.getSelectedItem().toString();
> >                         player = MediaPlayer.create(myContext,
> >                                         
> > soundTable.get(previewSoundName).intValue());
> >                 player.start();
> >                 return true;
> >                 }
> >         }
>
> > }
>
> > ====================================================
> > soundlibrary.xml - in res/layout
> > ====================================================
>
> > <?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:gravity="center"
> >   android:orientation="vertical">
>
> > <TextView
> > android:layout_width="wrap_content"
> > android:layout_height="wrap_content"
> > android:id="@+id/txtTitle"
> > android:text="@string/sound_library_title"></TextView>
>
> > <ListView
> > android:id="@+id/lstSounds"
> > android:layout_width="fill_parent"
> > android:layout_height="fill_parent"
> > android:choiceMode="multipleChoice"
> > android:clickable="true"
> > android:focusable="true"
> > android:longClickable="true"></ListView>
>
> > <TextView
> > android:layout_width="wrap_content"
> > android:layout_height="wrap_content"
> > android:id="@+id/txtPreview"
> > android:text="@string/sound_library_preview"></TextView>
>
> > </LinearLayout>
>
> > ====================================================
> > sounds.xml  - in res/values
> > ====================================================
> > <?xml version="1.0" encoding="utf-8"?>
> > <resources>
> >         <string-array name="sound_name">
> >                 <item>Big Sound</item>
> >                 <item>Little Sound</item>
> >         </string-array>
> >         <string-array name="sound_location">
> >                 <item>test_big_sound</item>
> >                 <item>test_little_sound</item>
> >         </string-array>
> > </resources>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to