如何在扩展ArrayAdapter <String>的类中获取共享首选项

roi*_*erg 2 android android-arrayadapter sharedpreferences

我有一个ArrayAdapter,我需要获得应用程序的共享首选项,以便用它做一些事情.我的问题我不能得到共享的偏好.那就是我的代码:

public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private static SharedPreferences prefs;
private String firstBtName = "";
private String secondBtName = "";
private String thirdBtName = "";


public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list, values);
    this.context = context;
    this.values = values;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    prefs = getSharedPreferences("MYPREFS", 0); // ERROR **************

    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    textView.setText(values[position]);
Run Code Online (Sandbox Code Playgroud)

eclips说我没有那种方法......我该怎么办?谢谢!

Foa*_*Guy 17

你的错误是因为ArrayAdapter没有getSharedPreferences()方法,更改给你错误的行:

prefs = context.getSharedPreferences("MYPREFS", 0); 
Run Code Online (Sandbox Code Playgroud)