删除后按后退按钮更新购物车计数
Posted
技术标签:
【中文标题】删除后按后退按钮更新购物车计数【英文标题】:Updating cart count on pressing of back button after deletion 【发布时间】:2016-03-28 11:43:24 【问题描述】:我有两个活动,一个是UserActivity
,另一个是CartActivity
我在UserActivity
中显示产品列表。单击按钮AddtoCart
我将产品添加到购物车。我正面临这个问题:
当我添加单击按钮 AddtoCart
时,操作栏中会出现一个购物车图标,并且我有一个 textview
的自定义布局,在该购物车图标上显示购物车计数器。每当我单击按钮AddtoCart
时,该计数器就会更新。现在我转到CartActivity
并从购物车中删除一些产品。当我现在按返回按钮返回到UserActivity
时,计数器文本视图不会更新。
我已经阅读了一些关于在Back button and refreshing previous activity 的问题中给出的按下后退更新的方法。答案中给出的两种方法是覆盖UserActivity
的OnResume()
方法或通过为结果启动活动。
当我按下后退按钮并从计数器TextView
中的原始产品数量中减去它并更新文本视图时,我想我需要将一个名为DeleteCounter
的变量从CartActivity
传递给UserActivity
.
这是UserActivity
的部分代码,我只有在单击按钮时才会调用此代码中更新购物车计数器的功能。 onActivityResult()
的代码也对此进行了评论,这是我从上面给出的 SO 问题链接的答案中尝试的。不成功:
public class UserActivity extends AppCompatActivity
private int cartindex = 0;
private TextView counterTV = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// @Override
// protected void onActivityResult(int requestCode, int resultCode, Intent data)
// if (requestCode == 1)
//
// if(resultCode == RESULT_OK)
// Intent intent = getIntent();
// Integer deletecounter = intent.getIntExtra("DeleteCounter",0);
// if(deletecounter>0)
// UpdateCartCount(Integer.parseInt(counterTV.getText().toString())-deletecounter);
//
//
//
//
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user, menu);
final View menu_list = menu.findItem(R.id.action_hawk).getActionView();
counterTV = (TextView) menu_list.findViewById(R.id.cartcounter);
UpdateCartCount(cartindex);
new MyMenuItemStuffListener(menu_hotlist, "Show message")
@Override
public void onClick(View v)
Intent intent= new Intent(UserActivity.this,CartActivity.class);
intent.putExtra("ProductTitle",pname);
intent.putExtra("ProductUrl",purl);
intent.putExtra("ProductPrice",pprice);
intent.putExtra("BargainPrice",bargainprice);
UserActivity.this.startActivity(intent);
;
return true;
//Function to update cart count
public void UpdateCartCount(final int new_number)
cartindex = new_number;
if (counterTV == null) return;
runOnUiThread(new Runnable()
@Override
public void run()
if (new_number == 0)
counterTV.setVisibility(View.INVISIBLE);
else
counterTV.setVisibility(View.VISIBLE);
counterTV.setText(Integer.toString(new_number));
);
这里是CartActivity
的代码:
public class CartActivity extends AppCompatActivity
private List<Product> mCartList;
private ProductAdapter mProductAdapter;
private static List<Product> cart;
private static Integer deletecounter= 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mCartList = getCart();
Intent intent = getIntent();
String ProductTitle = intent.getStringExtra("ProductTitle");
String ProductUrl = intent.getStringExtra("ProductUrl");
String ProductPrice = intent.getStringExtra("ProductPrice");
String BargainPrice = intent.getStringExtra("BargainPrice");
Product product = new Product(ProductTitle, ProductUrl, ProductPrice, BargainPrice);
mCartList.add(product);
// Make sure to clear the selections
for (int i = 0; i < mCartList.size(); i++)
mCartList.get(i).selected = false;
// Create the list
final ListView listViewCatalog = (ListView) findViewById(R.id.cart_list_view);
mProductAdapter = new ProductAdapter(mCartList, getLayoutInflater(), true, CartActivity.this);
listViewCatalog.setAdapter(mProductAdapter);
listViewCatalog.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
Product selectedProduct = mCartList.get(position);
if (selectedProduct.selected)
selectedProduct.selected = false;
else
selectedProduct.selected = true;
mProductAdapter.notifyDataSetInvalidated();
);
FloatingActionButton Delete = (FloatingActionButton) findViewById(R.id.fab);
delete.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
// Loop through and remove all the products that are selected
// Loop backwards so that the remove works correctly
for (int i = mCartList.size() - 1; i >= 0; i--)
if (mCartList.get(i).selected)
mCartList.remove(i);
deletecounter++;
// THIS IS THE CODE I USED TO RETURN DATA TO PREVIOUS ACTIVITY BUT UserActivity STARTS AUTOMATICALLY AFTER DELETION OF SELECTED PRODUCTS AS SOON AS I CLICK THE DELETE BUTTON EVEN WHEN THERE ARE PRODUCTS IN THE CART.
// if(deletecounter!=0)
// Intent i = new Intent(HawkActivity.this, UserActivity.class);
// startActivityForResult(i, 1);
// Intent returnIntent = new Intent();
// returnIntent.putExtra("DeleteCounter", deletecounter);
// setResult(RESULT_OK, returnIntent);
//
mProductAdapter.notifyDataSetChanged();
Snackbar.make(view,"Selected items deleted successfully",Snackbar.LENGTH_SHORT).show();
);
public static List<Product> getCart()
if(cart == null)
cart = new Vector<Product>();
return cart;
当我使用在两个活动中都被注释掉的代码时,即使用 start 活动作为结果方法时,会发生这种情况:
当我单击删除按钮时,项目会被删除,但 CartActivity
会自动关闭。即使购物车中有产品,带有计数器文本视图的 UserActivity
也会显示为“0”值。
请告诉我您需要从代码中获得的任何其他信息。欢迎在删除CartActivity
中的某些项目后按下后退按钮更新购物车计数器的任何其他方法。任何帮助表示赞赏。
【问题讨论】:
【参考方案1】:使用
invalidateOptionsMenu();
在onActivityResult
中再次填充菜单。
你的UserActivity
代码应该是这样的:
public class UserActivity extends AppCompatActivity
private int cartindex = 0;
private TextView counterTV = null;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == 1)
if (resultCode == RESULT_OK)
Intent intent = getIntent();
Integer deletecounter = intent.getIntExtra("DeleteCounter",0);
if (deletecounter>0)
cartindex=Integer.parseInt(counterTV.getText().toString())-deletecounter;
invalidateOptionsMenu();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user, menu);
final View menu_list = menu.findItem(R.id.action_hawk).getActionView();
counterTV = (TextView) menu_list.findViewById(R.id.cartcounter);
UpdateCartCount(cartindex);
new MyMenuItemStuffListener(menu_hotlist, "Show message")
@Override
public void onClick(View v)
Intent intent= new Intent(UserActivity.this,CartActivity.class);
intent.putExtra("ProductTitle",pname);
intent.putExtra("ProductUrl",purl);
intent.putExtra("ProductPrice",pprice);
intent.putExtra("BargainPrice",bargainprice);
UserActivity.this.startActivity(intent);
;
return true;
//Function to update cart count
public void UpdateCartCount(final int new_number)
cartindex = new_number;
if (counterTV == null) return;
runOnUiThread(new Runnable()
@Override
public void run()
if (new_number == 0)
counterTV.setVisibility(View.INVISIBLE);
else
counterTV.setVisibility(View.VISIBLE);
counterTV.setText(Integer.toString(new_number));
);
@Override
protected void onRestart()
if (CartActivity.cart.size()!=0)
cartindex=CartActivity.cart.size();
invalidateOptionsMenu();
super.onRestart()
【讨论】:
不,仍然无法正常工作。问题是当我按下删除按钮时,选定的产品被删除并且CartActivity
自行完成,即使产品仍在购物车中并且UserActivity
出现在购物车中的计数器为零。单击删除时,我根本不希望出现UserActivity
!如果我按回来,它应该会显示更新后的购物车计数。
在 CartActivity 中用 if(mCartList.size()==0) 替换 if(deletecounter!=0) 条件
UserActivity 被调用,因为当满足条件 deletecounter!=0 的项目被删除并且 Intent 被触发时,删除计数器会增加,因此最好检查 arraylist 的大小是否为 0,如果为 0,则只有 Intent to UserActivity 应该是解雇
完成。解决了单击删除按钮时启动“UserActivity”的问题。计数器问题仍然存在。假设我在购物车中添加了 2 件商品,购物车计数器显示 2。我移动到购物车活动并删除了一件商品。然后我按下后退按钮购物车计数器仍然显示 2。这个问题仍然没有解决。
ok 调用 invalidateOptionsMenu();在 UserActivity 的 onResume() 方法中以上是关于删除后按后退按钮更新购物车计数的主要内容,如果未能解决你的问题,请参考以下文章