存储List

private void  Save_List(List<String> arrayList, String s){
        SharedPreferences sp=getContext().getSharedPreferences(s,Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("size", arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            edit.remove("type"+i);
            edit.putString("type" + i, arrayList.get(i));
        }
        edit.apply();
    }

读取List

private void Get_List(List<String> arrayList,String s){
        SharedPreferences sp=getContext().getSharedPreferences(s,Context.MODE_PRIVATE);
        int size=sp.getInt("size",0);
        for(int i=0;i<size;i++) {
            arrayList.add (sp.getString("type"+i,null));
        }
    }

其他

fragment中获取app版本号

String version_code=null;
                try{
                    PackageManager packageManager=getActivity().getPackageManager();
                    PackageInfo info=packageManager.getPackageInfo(getActivity().getPackageName(),0);
                    version_code=info.versionName;
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }

Preference设置添加点击事件

findPreference("about").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                return false;
            }
        });

同时在xml文件中添加key:

<PreferenceCategory
        android:layout="@layout/preference_category_widget"
        android:title="更多">
        <Preference
            android:key="version"
            android:title="版本"
            android:summary="V1.0"
            android:layout="@layout/preference_item"/>

        <Preference
            android:key="about"
            android:layout="@layout/preference_item"
            android:title="关于"
            android:summary="About the appliacation"/>
    </PreferenceCategory>
Last modification:July 21st, 2020 at 10:48 pm