开发者

Android ListPreference Fill Item onPreferenceClick Event

How to Fill ListPreference entry and entryvalues From onPreferenceClick event

I Filled From onPreferenceClick event But When First Clicked Getting Empty List When Second Clicked is Getting All Item.

开发者_如何学编程

Why When First Cliked Then Getting Empty List?

thanks.


When setOnPreferenceClickListener is called the dialog seems already created. A possible solution is to extend ListPreference and intercept the click on the view.

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;
    }

}

The new class have the public method setOnClickListner, you should use it instead of setOnPreferenceClickListener.

Then you can use DynamicListPreference in your xml preference file:

<?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>

In your 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"});
        }
    };

}


The first idea that popped in my mind. Looks like a very dirty hack to get around, but anyway:

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;
        }
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜