tags:

views:

22

answers:

0

Hi.

I have a ListPreference which I populate dynamically when clicking on the list to display it. The population works fine, but the values I populate is not displayed until the next time i click to open the list, but instead the values from the xml-file is displayed the first time. It feels like the list has already been populated before the onPreferenceClick is called. How can I refresh the list probably? I want to populate the list before every click in the list.

     /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  addPreferencesFromResource(R.xml.preferences);

  // Get preferences.
  prefs = PreferenceManager.getDefaultSharedPreferences(this);
  screen = getPreferenceScreen();

  // Install on-click listener for calendars.
  screen.findPreference("googleCalendars").setOnPreferenceClickListener(onSelectCalendarsClick);
 }

 /**
  * We had a click on select calendars.
  */
 public OnPreferenceClickListener onSelectCalendarsClick = new OnPreferenceClickListener() {
  public boolean onPreferenceClick(Preference pref) {

   final String username;
   final String password;
   final String list;

   if (pref == findPreference("googleCalendars")) {
    username = prefs.getString("googleUsername", "");
    password = prefs.getString("googlePassword", "");
    list = "googleCalendars";
   }
   else {
    Log.d(TAG, "onSelectCalendarsClick.run(),unknown list clicked: " + pref.getTitle());
    return false;
   }

   // Show process dialog while updating.
      final ProgressDialog dialog = ProgressDialog.show(Preferences.this, "", "Fetching calendars, please wait...", true);
      // Show dialog, non cancelable.
      dialog.setCancelable(false);
       dialog.show();

   // Create new updater thread.
   new Thread(){
    public void run() {
     Log.d(TAG, "onSelectCalendarsClick.run(), new thread. Fetching calendars...");
     // Get calendars
     // Create API.
     CalendarApi calendarApi = new CalendarApi(prefs.getBoolean("debugFakeCalendar", false));
     GetCalendarsResponse response = calendarApi.GetCalendars(username, password);
     Log.d(TAG, "onSelectCalendarsClick.run(), new thread. Done!");
     // Create new message and send it.
     Message msg = new Message();
     Bundle bundle = new Bundle();
     bundle.putString("list", list);
     msg.setData(bundle);
     msg.obj = response;
     getCalendarsHandler.sendMessage(msg);
     // cancel dialog.
     dialog.cancel();
    }
   }.start();

   return false;
  }

 };

 /**
  * Handler for processing response from getCalendars.
  */
    Handler getCalendarsHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);


        // We have a new message.
        GetCalendarsResponse response = (GetCalendarsResponse)msg.obj;
        Bundle bundle = msg.getData();

        // Null?
        if (response == null || response.calendars == null) {
          Log.d(TAG, "getCalendarsHandler.handleMessage(): response is null.");
          return;
        }

         // Fetch.
        ListPreferenceMultiSelect list = (ListPreferenceMultiSelect)screen.findPreference(bundle.getString("list"));
        if (list != null) {
          // Ok response.
          list.setEntries(response.getEntries());
          list.setEntryValues(response.getEntryValues());
        }

        // Display list preference.
        list.getDialog().show(); 
      }
    };
}