Android:如何获取我的应用程序的所有偏好 xml 列表并阅读它们? [英] Android: how to get list of all preference xml's for my app and read them?
问题描述
如何获取应用程序的所有应用程序首选项列表,
1. 我正在以这种方式保存共享首选项
2. 我知道它们在 data/data/app_packagename/shared_prefs
3.问题: 但是如何在微调器中获取所有首选项 xml 文件的列表
并阅读每个首选项,我在 SO 中搜索,但我没有找到任何有关此的帮助,如何读取所有首选项 xml 文件我的应用程序目录并访问首选项?
PS:我知道 SharedPreference.getAll();
,一旦我拿到文件就足够阅读了吗?>
我用bits(Rough Code)写的,运行时报错,下面是实现方法
void getList(){//将从 onCreate 调用以填充微调器,是的微调器已绑定PackageManager m = getPackageManager();String s = getPackageName();尝试 {PackageInfo p = m.getPackageInfo(s, 0);s = p.applicationInfo.dataDir;} catch (NameNotFoundException e) {Log.w("yourtag", "未找到错误包名", e);}Log.i("dir", s=s+"/shared_prefs");//是这种写法,怎么从这里开始}
试试这个
File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");if(prefsdir.exists() && prefsdir.isDirectory()){String[] list = prefsdir.list();ArrayAdapter适配器 = 新的 ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1,list);Spinner sp = (Spinner) findViewById(R.id.spinner1);sp.setAdapter(适配器);}
//获取选中的item
String item = (String) sp.getSelectedItem();//从文件名中删除.xmlString preffile = item.substring(0, item.length()-4);SharedPreferences sp2 = getSharedPreferences(preffile, MODE_PRIVATE);映射<字符串,?>地图 = sp2.getAll();for (Entry entry : map.entrySet()){System.out.println("key为"+ entry.getKey() + " and value is " + entry.getValue());}
how to get list of all application preferences for application,
1. I am saving shared preference in this manner
2. I know that they are in data/data/app_packagename/shared_prefs
3. THE PROBLEM: But how to get list of all preference xml files in a spinner
and read each preference, i searched in SO, but i did not found any help regarding this, how to do read all preference xml files in my application directory and access the preferences?
P.S: I am aware of SharedPreference.getAll();
, will be enough to read once i get the file?
I have wrote in bits(Rough Code), it give error when tried to run, here is the implemented method
void getList()
{
//will be invoked from onCreate to populate spinner,yes spinner is already binded
PackageManager m = getPackageManager();
String s = getPackageName();
try {
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (NameNotFoundException e) {
Log.w("yourtag", "Error Package name not found ", e);
}
Log.i("dir", s=s+"/shared_prefs");
//is this write way, how to proceed from here
}
Try this
File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");
if(prefsdir.exists() && prefsdir.isDirectory()){
String[] list = prefsdir.list();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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<String, ?> map = sp2.getAll();
for (Entry<String, ?> entry : map.entrySet()){
System.out.println("key is "+ entry.getKey() + " and value is " + entry.getValue());
}
这篇关于Android:如何获取我的应用程序的所有偏好 xml 列表并阅读它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!