我有自定义的SharedPreference类,并且尝试从我的自定义SharedPrefecenses类获取字符串。当“ prf = new PrefManager(context);”
时,getView方法出现错误该怎么办,我已经搜索了2天。
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "Secur-transact";
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public String getString(String PREF_NAME) {
if(pref.contains(PREF_NAME)){
return pref.getString(PREF_NAME,null);
}
return "";
}
}
我尝试在适配器视图中获取getString方法。
public class CustomListAdapter extends BaseAdapter {
PrefManager prf;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
prf = new PrefManager(context); // I GOT ERROR IN HERE
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView) convertView.findViewById(R.id.name);
if (prf.getString("ColorName").equals("RED")) {
name.setBackgroundResource(R.color.primaryLightColorRed);
name.setTextColor(R.color.colorWhite);
}
在这里初始化
public class SampleClass extends Fragment {
private List<ModelsProps> modelsPropsList = new ArrayList<ModelsProps>();
public SampleClass () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final ListView listView = (ListView) rootView.findViewById(R.id.list);
final CustomListAdapter adapter = new CustomListAdapter(getActivity(), modelsPropsList);
listView.setAdapter(adapter);
答案 0 :(得分:0)
您可以将上下文从片段传递到构造器中的适配器,然后将其从适配器传递到首选项类。
答案 1 :(得分:-1)
您可能没有初始化变量last_local_call_time < DATE_SUB(NOW(), INTERVAL 30 DAY)
。
只需尝试更改行:
context
收件人:
prf = new PrefManager(context);