使用首选项活动过滤对象

时间:2016-10-11 08:57:07

标签: android filter android-preferences

我的应用程序中有一个首选项活动,我从首选项列表中进行选择。选择我的练习后,我想使用该选定的值通过练习片段来过滤我的数据。我能够在设置屏幕上获得我选择的值,但它不会过滤。

这是我到目前为止所做的:

PreferenceActivity:

@Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        entries = new ArrayList<>();
        entryValues = new ArrayList<>();
        practicesListPreference = (ListPreference) findPreference("my_practice_setting");

        populatePracticeListPreference();

        SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
        sp.edit().putString(KEY_PREF_MY_PRACTICE_ID, JsonDataParser
                .practiceNameAndIdMap.get(sp.getString(KEY_PREF_MY_PRACTICE, ""))).apply();
        String s = sp.getString(KEY_PREF_MY_PRACTICE_ID,"NONE");

        practicesListPreference.setSummary(sp.getString(KEY_PREF_MY_PRACTICE, "All"));

        int storedPreference = sp.getInt("storedInt", 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt("storedInt", storedPreference); // value to store
        editor.commit();
    }

这是我用来过滤的片段:

 practiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());

            String pid=sharedPreferences.getString(PreferencesActivity.KEY_PREF_MY_PRACTICE,"");
                if(practiceSpinnerAdapter.isEmpty()){
                    practiceSpinnerAdapter.add("All");
                }

                pid = practiceSpinnerAdapter.getItem(i);
                mAdapter.clear();
                mAdapter.addAll(filterAssetsList(pid));

和我在片段中的过滤方法:

   private List<Asset> filterAssetsList(String filterString){
    List<Asset> filteredAssets = new ArrayList<>();
    if(JsonDataParser.assetsList!=null){
        if(filterString.equals("All")){
           return JsonDataParser.assetsList;
        }
        for (Asset asset: JsonDataParser.assetsList ) {
           // String s= asset.getPracticeID();
            if(asset.getPracticeID().equals(filterString)){
                filteredAssets.add(asset);
            }
        }
    }
    return filteredAssets;
}

我做错了什么?

0 个答案:

没有答案