bra*_*ker 6 java android cart onactivityresult startactivityforresult
我有两个活动,一个是UserActivity,另一个是CartActivity我正在展示产品清单UserActivity.点击按钮,AddtoCart我将产品添加到购物车.我正面临这个问题:
当我添加单击按钮AddtoCart时,操作栏中有一个购物车图标,我textview在该购物车图标上有一个显示购物车计数器的自定义布局.每当我点击按钮时,该计数器就会更新AddtoCart.现在我转到CartActivity并删除购物车中的一些产品.现在按回按钮返回时UserActivity,计数器文本视图不会更新.
我已经阅读了几个关于按下后退的更新的方法,如问题中的" 返回"按钮和刷新之前的活动.答案中给出的两种方法是通过覆盖结果的OnResume()方法UserActivity或启动结果的活动.
我想我需要将一个名为DeleteCounterfrom 的变量传递CartActivity给UserActivity当我按下后退按钮并从计数器中的原始产品数中减去它TextView并更新文本视图.
这是部分代码,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));
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是代码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;
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用在两个活动中注释掉的代码,即使用结果方法的启动活动时,会发生这种情况:当我单击删除按钮时,项目会被删除,但会CartActivity自动关闭.在UserActivity与计数器文本视图被示出具有即使存在产品在车"0"值.
请告诉我您需要从代码中获得的任何其他信息.在删除某些项目后,我可以通过按下后退按钮来更新购物车计数器的任何其他方式CartActivity.任何帮助表示赞赏.
使用
invalidateOptionsMenu();
Run Code Online (Sandbox Code Playgroud)
再次填充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()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |