我想将一个字符串替换为从共享首选项中提取的另一个字符串:
public class DisplayAdapter extends BaseAdapter {
private final static String MY_PREFERENCES = "MyPref";
private final static String GESTIONE = "Gestione";
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> gestioneName;
public DisplayAdapter(Context c, ArrayList<String> gestione,ArrayList<String> id) {
this.mContext = c;
this.id = id;
this.gestioneName = gestione;
}
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
String Gestione = prefs.getString(GESTIONE, "Entrata");
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
mHolder = new Holder();
mHolder.txt_gestione = (TextView) child.findViewById(R.id.txt_gestione);
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
我想替换
String Gestione = "Entrata";
替换它:
SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
String Gestione = prefs.getString(GESTIONE, "Entrata");
在dispay适配器中,如果我放置来自共享首选项类的数据字符串给我一个仙女错误的字符串行, 我该如何解决?
答案 0 :(得分:0)
确保在Adapter的构造函数中声明Context
。
private Context context
public CustomAdapter(Context ctx...) {
this.context = ctx;
...
}
public View getView(int post, View child, ViewGroup parent) {
SharedPreferences prefs = context.getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
String Gestione = prefs.getString(GESTIONE, "Entrata");
...
}