Android: how to get list of all preference xml's for my app and read them?

前端 未结 3 404
挽巷
挽巷 2020-12-16 04:16

how to get list of all application preferences for application,

1. I am saving shared preference in this manner

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-16 05:05

    Try this

        File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");
    
        if(prefsdir.exists() && prefsdir.isDirectory()){
            String[] list = prefsdir.list();
            ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1,list);
            Spinner sp = (Spinner) findViewById(R.id.spinner1);
            sp.setAdapter(adapter);
    
        }
    

    //To get the selected item

    String item = (String) sp.getSelectedItem();
        //remove .xml from the file name
        String preffile = item.substring(0, item.length()-4);
    
        SharedPreferences sp2 = getSharedPreferences(preffile, MODE_PRIVATE);
        Map map = sp2.getAll();          
    
        for (Entry entry : map.entrySet()){
            System.out.println("key is "+ entry.getKey() + " and value is " + entry.getValue());
        }
    

提交回复
热议问题