gpt4 book ai didi

android - 刷新 ListPreference

转载 作者:行者123 更新时间:2023-11-30 04:49:49 24 4
gpt4 key购买 nike

我有一个 ListPreference,我在单击列表以显示它时动态填充它。填充工作正常,但直到我下次单击打开列表时才会显示我填充的值,而是第一次显示 xml 文件中的值。感觉就像在调用 onPreferenceClick 之前已经填充了列表。我怎样才能刷新列表?我想在每次点击列表之前填充列表。

     /** 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();
}
};
}

最佳答案

setEntries 的文档指出设置将显示在后续对话框中。

一旦 OnClickPreference 被触发,Android 就会填充 ListPreference,您之后所做的任何操作都将显示在后续对话框中。

您可能希望预先填充它,而不是在显示后刷新它。预先填充它的最佳方法是在 onCreate 中填充 ListPreference,并让它在那里显示。

希望对你有帮助

关于android - 刷新 ListPreference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3839114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com