[英]Retrieving a shared preference inside a baseexpandablelistadapter
我正在写一个baseexpandablelistadapter。 在其中,我想执行一个sqlite查询,我需要一个字符串作为搜索参数传递。 我已将此字符串存储在共享首选项中,现在我想要检索它。 不幸的是,我似乎无法做到这一点。
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
ClientSmartFinderSettings child = (ClientSmartFinderSettings) getChild(groupPosition, childPosition);
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.expandlist_child_item, null);
holder.checkBox = (CheckBox) view.findViewById(R.id.check_box);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
try {
//this is where the error is
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
holder.checkBox.setChecked(child.getIsSelected());
} catch (Exception e) {
}
return view;
}
错误是:
MODE_PRIVATE无法解析为变量
我知道MODE_PRIVATE似乎只适用于Activity。 但是,如何在baseexpandablelistadapter中检索此共享首选项?
谢谢!
首先,在AdapterView中创建和绑定视图时执行查询不是一个好习惯,但要回答你的问题:
SharedPreferences sharedPreferences = context.getSharedPreferences("FileName", Context.MODE_PRIVATE);
另外,我会在Adapter类的构造函数中创建一次SharedPreferences对象(您可以在那里传递Context对象)。 这将删除滚动的hickups。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.