在OnCreate
我有这个productsku,我想在完成后删除存储的数据,preferences.edit().remove("Productsku").commit();
我把它们放在正确的位置?当我做了与内容相关的事情时,我检查了LogCat,它仍然让我进入" if (productsku != null)
"
public class ProductActivity extends FragmentActivity implements
OnFocusChangeListener, OnClickListener, NumberPickerDialogListener,
AddedToCartDialogListener {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(getClass().getSimpleName(), "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
SharedPreferences preferences1 = PreferenceManager.getDefaultSharedPreferences(this);
content=preferences1.getString("Content", "");
SharedPreferences preferences = getSharedPreferences("myPref",MODE_PRIVATE);
productsku=preferences.getString("Productsku", "");
Log.d("Productsku",productsku.toString());
if(productsku !=null)
{
Log.d("StartProductsku","OK");
new GetProductDetailsTask2().execute();
preferences.edit().remove("Productsku").commit();
Log.d("EndProductsku","OK");
}
else
{
if(content !=null)
{
Log.d("StartContent","OK");
new GetProductDetailsTask1().execute();
preferences1.edit().remove("Content").commit();
}
private class GetProductDetailsTask2 extends AsyncTask<String, String, Void> {
@Override
protected void onPreExecute() {
layoutProgress.setVisibility(View.VISIBLE);
};
@Override
protected Void doInBackground(String... params) {
if(productsku ==null)
{
Log.d("Productsku","inside");
product = Product.getSelected();
product.setId(productsku);
JSONService.updateProductDetails(product);
}
else
{
product=JSONService.getProductDetails(productsku);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
updateUI();
layoutProgress.setVisibility(View.GONE);
shortDescription.setVisibility(View.VISIBLE);
description.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:2)
你的问题在这里:
SharedPreferences preferences = getSharedPreferences("myPref",MODE_PRIVATE);
productsku=preferences.getString("Productsku", "");
getString
中的第二个参数是默认值(如果未找到首选项,则返回值)。尝试执行以下操作:
productsku=preferences.getString("Productsku", "");
Log.d("Productsku",productsku.toString());
if(!productsku.equals(""))
{
Log.d("StartProductsku","OK");
new GetProductDetailsTask2().execute();
preferences.edit().remove("Productsku").commit();
Log.d("EndProductsku","OK");
}
希望有所帮助