我有一个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说我没有那种方法......
我能做什么?谢谢!
答案 0 :(得分:17)
您的错误是因为ArrayAdapter没有getSharedPreferences()
方法,请更改给您错误的行:
prefs = context.getSharedPreferences("MYPREFS", 0);
答案 1 :(得分:0)
您应该在构造函数中获得共享的首选项。您的构造函数应如下所示
public MobileArrayAdapter(Context aplicationContext, String[] values) {
super(context, R.layout.list, values);
this.context = context;
this.values = values;
prefs= aplicationContext.getSharedPreferences("MYPREFS", 0);
}