Android 在baseexpandablelistadapter中检索共享首选项

Android 在baseexpandablelistadapter中检索共享首选项,android,sharedpreferences,Android,Sharedpreferences,我正在写一个baseexpandablelistadapter。在它里面,我想执行一个sqlite查询,我需要一个字符串作为搜索参数传递给它。我已将此字符串存储在共享首选项中,现在我想检索它。不幸的是,我似乎无法做到这一点 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) { ClientSmartFind

我正在写一个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;
    }
错误是:

无法将模式_PRIVATE解析为变量

我理解,MODE_PRIVATE似乎只适用于某项活动。但是,如何在baseexpandablelistadapter中检索此共享首选项


谢谢

首先,在AdapterView中创建和绑定视图时执行查询不是一个好的做法,但要回答您的问题:

SharedPreferences sharedPreferences = context.getSharedPreferences("FileName", Context.MODE_PRIVATE);

此外,我将在适配器类的构造函数中创建SharedReferences对象一次(您可以在那里传递上下文对象)。这将消除滚动问题。

首先,在AdapterView中创建和绑定视图时执行查询不是一个好做法,但要回答您的问题:

SharedPreferences sharedPreferences = context.getSharedPreferences("FileName", Context.MODE_PRIVATE);

此外,我将在适配器类的构造函数中创建SharedReferences对象一次(您可以在那里传递上下文对象)。这将删除滚动提示。

应该是
Context.MODE\u PRIVATE
。感谢您的建议。我试过了。它只是将错误更改为“类型ExpandListAdapter的getSharedReferences(String,int)方法未定义”,您必须在适配器的构造函数中提供一个上下文参数,并将其用作
mContext.getSharedReferences(String,int)应该是
上下文。MODE\u PRIVATE
。谢谢你的建议。我试过了。它只是将错误更改为“类型ExpandListAdapter的getSharedReferences(String,int)方法未定义”,您必须在适配器的构造函数中提供一个上下文参数,并将其用作
mContext.getSharedReferences(String,int)