我正在尝试使用共享首选项数据创建一个列表视图,并且每次我必须添加一个数据来重新填充 listivew,但它只获取输入的最新数据,因此列表视图中的显示往往具有1 row only every time
.
代码片段:
String strSavedMem1 = sharedPreferences.getString("food", "");
String[] values = new String[] { strSavedMem1 }; //That you got from your intent bundle
LVAdapter adapter = new LVAdapter(this, R.layout.foodlist, values);
setListAdapter(adapter);
LVAdapter.java
public class LVAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public LVAdapter(Context context, int foodlist, String[] values) {
super(context, R.layout.foodlist, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.foodlist, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.foods);
textView.setText(values[position]);
return rowView;
}
}
将值存储在共享首选项中的代码。
String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_FOODNAME));
food.setText(item_content1);
SavePreferences("food", food.getText().toString());
LoadPreferences();
}};
protected void SavePreferences(String key, String value) {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
protected void LoadPreferences() {
// TODO Auto-generated method stub
SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("food", "");
food.setText(strSavedMem1);
}
我必须在哪里更改上述代码,才能从共享偏好中获取全部数据?