如何从onPreferenceClick事件
填充ListPreference条目和条目值我从onPreferenceClick事件填充但是首次点击获取空列表时 当Second Seconded获得所有项目时。
为什么第一次登场然后获得空名单?
感谢。
答案 0 :(得分:4)
当调用setOnPreferenceClickListener时,似乎已经创建了对话框。一种可能的解决方案是扩展ListPreference并拦截视图上的点击。
package com.example;
import android.preference.ListPreference;
import android.content.Context;
import android.util.AttributeSet;
public class DynamicListPreference extends ListPreference {
public interface DynamicListPreferenceOnClickListener {
public void onClick(DynamicListPreference preference);
}
private DynamicListPreferenceOnClickListener mOnClicListner;
public DynamicListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onClick(){
if (mOnClicListner != null)
mOnClicListner.onClick(this);
super.onClick();
}
public void setOnClickListner(DynamicListPreferenceOnClickListener l) {
mOnClicListner = l;
}
}
新类具有公共方法 setOnClickListner ,您应该使用它而不是 setOnPreferenceClickListener 。
然后您可以在xml首选项文件中使用 DynamicListPreference :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<com.example.DynamicListPreference
android:key="id_list"
android:title="@string/opt_label"
/>
</PreferenceScreen>
在PreferenceActiviy中:
package com.example;
import com.example.DynamicListPreference.DynamicListPreferenceOnClickListener;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.os.Bundle;
public class MyPreferencesActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
DynamicListPreference pref = (DynamicListPreference)findPreference("id_list");
pref.setOnClickListner(onClickListener_mylist);
}
private DynamicListPreferenceOnClickListener onClickListener_mylist = new DynamicListPreferenceOnClickListener() {
@Override
public void onClick(DynamicListPreference preference) {
ListPreference pref = (ListPreference) preference;
pref.setEntryValues(new String[] {"1", "2"});
pref.setEntries(new String[] {"first", "second"});
}
};
}
答案 1 :(得分:3)
第一个出现在我脑海中的想法。看起来像是一个非常肮脏的黑客,但无论如何:
findPreference("yourPreferenceKey").setOnPreferenceClickListener(new OnPreferenceClickListener() {
private final String[] values = {"1","2","3","4"}; // Data set (as an example)
@Override
public boolean onPreferenceClick(final Preference preference) {
((ListPreference)preference).setEntryValues(values); // Set new values
((ListPreference)preference).setEntries(values); // And new keys
final OnPreferenceClickListener prefClickListener = this; // Store the reference to this click listener for a later use
preference.setOnPreferenceClickListener(null); // And remove it from current preference, so it won't cause a recursion when we will emulate user click later
YourPreferenceActivity.this.runOnUiThread(new Runnable(){ // Do some work after current listener finishes it's stuff
@Override
public void run() {
((ListPreference)preference).getDialog().dismiss(); // Dismiss the dialog that popped up because of our first click (with the old data)
YourPreferenceActivity.this.getPreferenceScreen().onItemClick(null, null, preference.getOrder(), 0); // Emulate user click. System will think that user clicked on the same preference again
preference.setOnPreferenceClickListener(prefClickListener); // Let's return our click listener back, so the whole operation will repeat on the next click (delete thi sline if you need preloading only once.
}
});
return false;
}
});