这是我的代码
在偏好设置中设置值:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView=inflater.inflate(resource, null);
holder.tvItemName = (TextView) convertView.findViewById(R.id.tvItemName);
holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
holder.bLess = (Button) convertView.findViewById(R.id.bLess);
holder.bAdd = (Button) convertView.findViewById(R.id.bAdd);
holder.etItemQuantity = (EditText) convertView.findViewById(R.id.etItemQuantity);
holder.bAddToCart = (Button) convertView.findViewById(R.id.bAddToCart);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvItemName.setText(itemModelList.get(position).getItemName());
holder.tvPrice.setText("Rs " + itemModelList.get(position).getSalesRate());
holder.bAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int qty = Integer.parseInt(holder.etItemQuantity.getText().toString().trim());
qty++;
holder.etItemQuantity.setText(""+qty);
}
});
holder.bLess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int qty = Integer.parseInt(holder.etItemQuantity.getText().toString().trim());
if(qty != 0){
qty--;
holder.etItemQuantity.setText(""+qty);
}
}
});
final String itemName = itemModelList.get(position).getItemName();
final String salesRate = itemModelList.get(position).getSalesRate();
holder.bAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("itemName", itemName );
edit.putString("salesRate", salesRate );
edit.commit();
Toast.makeText(Items.this,"Added item to cart", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
从偏好中检索数据:
public class CartDisplay extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_cart_display);
TextView tvItemName = (TextView) findViewById(R.id.tvItemName);
TextView tvSalesRate = (TextView) findViewById(R.id.tvSalesRate);
TextView tvTotalAmount = (TextView) findViewById(R.id.tvTotalAmount);
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String itemName = prefs.getString("itemName", "No itemName defined");//"No name defined" is the default value.
String salesRate = prefs.getString("salesRate", "0");//"0" is the default value.
tvItemName.setText(itemName);
tvSalesRate.setText("RS "+salesRate);
tvTotalAmount.setText("Total: RS" +salesRate);
}
}
idk为什么我的代码不起作用
真的需要帮助,here is my full code
答案 0 :(得分:1)
在那里创建Application
类存储qty变量并访问app中的任何位置。
正如您所指的那样,if (restoredText != null) {
可能会用来检查SharedPreference
是否有价值。用户试图避免为每个值检查 null 。所以他为“text”设置了一个值
您没有设置文本值只需粘贴下面的代码删除if语句。
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0);
我认为
String restoredText = prefs.getString("text", null);
返回 null ,因此if
语句未执行
if (restoredText != null) { ...}
答案 1 :(得分:1)
我在你的代码中发现了一些逻辑错误。这是
String restoredText = prefs.getString("text", null);
// you are trying to fetch the value of "text" but you had forgot to put /save the "text" value in your code
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("itemName", itemName );
edit.putString("salesRate", salesRate );
edit.commit();
解决方案:在" text"中添加一些值,在保存期间尝试再次获取。 喜欢这个
SharedPreferences prefs = getSharedPreferences("my_prefs", MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("text", "text" );
edit.putString("itemName", itemName );
edit.putString("salesRate", salesRate );
edit.commit();
String restoredText = prefs.getString("text", null);
if (restoredText.equals("text")) {
String itemName = prefs.getString("itemName", "No itemName defined");//"No name defined" is the default value.
String salesRate = prefs.getString("salesRate", "0");//"0" is the default value.
tvItemName.setText(itemName);
tvSalesRate.setText("RS "+salesRate);
tvTotalAmount.setText("Total: RS" +salesRate);
}
答案 2 :(得分:0)
我认为问题在于这一行
String restoredText = prefs.getString("text", null);
if (restoredText != null) { ... }
我不知道你把这个值放在sharepreference中的哪个位置,你在这里检查!= null,所以永远不会调用该块中的代码。
答案 3 :(得分:0)
使用以下方法获取和设置sharedPreferences:
static public boolean setPreference(Context c, String value, String key) {
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
settings = c.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
static public String getPreference(Context c, String key) {
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
settings = c.getSharedPreferences(PREFS_NAME, 0);
String value = settings.getString(key, "");
return value;
}
答案 4 :(得分:0)
我认为您需要在SharedPreference中存储值时使用应用程序上下文。
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();