3

我有一个 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]);

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

4

2 回答 2

17

您的错误是因为 ArrayAdapter 没有getSharedPreferences()方法,请将出现错误的行更改为:

prefs = context.getSharedPreferences("MYPREFS", 0); 
于 2012-06-03T13:40:52.447 回答
0

您应该在构造函数中获得共享首选项。您的构造函数应如下所示

public MobileArrayAdapter(Context aplicationContext, String[] values) {
    super(context, R.layout.list, values);
    this.context = context;
    this.values = values;
    prefs= aplicationContext.getSharedPreferences("MYPREFS", 0);
}
于 2018-07-06T21:25:39.017 回答