Android的:如何让所有的preference XML的名单对我的应用程序和阅读? [英] Android: how to get list of all preference xml's for my app and read them?
问题描述
如何让所有的应用程序preferences应用程序列表,
1 我现在的储蓄共享preference以这种方式
2 我知道,他们是在数据/数据/ app_packagename / shared_ preFS
3。问题:可是如何才能让在旋转的所有preference XML文件的列表
和读取每个preference,我搜索在 SO ,但我没有发现这方面的任何帮助,怎么办读取所有$在我的应用程序目录p $ pference XML文件并访问preferences?
PS:我知道的共享preference.getAll();
,也就足够了读一旦我得到的文件?
我在位(粗糙code),试图运行时,它给错误写的,这里是实现的方法
无效的GetList()
{
//从的onCreate调用来填充微调,微调是已经绑定
软件包管理系统M = getPackageManager();
字符串s = getPackageName();
尝试{
PackageInfo p值= m.getPackageInfo(S,0);
S = p.applicationInfo.dataDir;
}赶上(ē的NameNotFoundException){
Log.w(yourtag,错误包名称找不到,E);
}
Log.i(目录,S = S +/ shared_ preFS);
//是这样写的方式,如何从这里出发
}
试试这个
文件prefsdir =新的文件(getApplicationInfo()DATADIR,shared_ preFS); 如果(prefsdir.exists()及&放大器; prefsdir.isDirectory()){
的String [] =列表prefsdir.list();
ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_expandable_list_item_1,android.R.id.text1,清单);
微调SP =(微调)findViewById(R.id.spinner1);
sp.setAdapter(适配器); }
//为了获得所选择的项目
字符串项=(字符串)sp.getSelectedItem();
//从文件名中删除的.xml
字符串preffile = item.substring(0,item.length() - 4); 共享preferences SP2 = getShared preferences(preffile,MODE_PRIVATE);
地图<字符串,>地图= sp2.getAll(); 为(?钥匙进入LT;弦乐,>项:map.entrySet()){
的System.out.println(键是+ entry.getKey()+和值+ 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的:如何让所有的preference XML的名单对我的应用程序和阅读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!