2015-12-22 137 views
6

我有兩個活動,一個是UserActivity和另一個是CartActivity我在UserActivity顯示產品的列表。點擊一個按鈕AddtoCart我正在將產品添加到購物車。我面臨這個問題:刪除後按下後退按鈕更新購物車計數

當我添加單擊按鈕AddtoCart有一個購物車圖標在操作欄中,我有一個textview自定義佈局顯示購物車圖標上的購物車計數器。只要點擊按鈕AddtoCart,計數器就會更新。現在我轉到CartActivity並從購物車中刪除一些產品。當我現在按回按鈕回到UserActivity時,計數器文本視圖不會更新。

我已經閱讀了關於按下背部的更新方式的幾種方法,如問題Back button and refreshing previous activity中給出的那樣。答案中給出的兩種方法是覆蓋UserActivityOnResume()方法或通過開始結果活動。

我想我需要從CartActivity通過一個名爲DeleteCounter變量來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)); 
          } 
         } 
        }); 
       } 

這裏是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; 
     } 
    } 

當我使用這兩個活動註釋掉的代碼,即使用啓動活動的結果的方法,這種情況: 當我點擊刪除按鈕時,項目被刪除,但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()} 
+0

沒有,仍然沒有工作。問題是當我按下刪除按鈕時,即使產品仍然存在於購物車中,並且'UserActivity'出現在購物車中的零計數器中,所選產品將被刪除並且'CartActivity'自行完成。當我點擊刪除時,我不希望'UserActivity'出現!如果我按回來,它應該顯示與購物車更新的計數。 – brainbreaker

+0

CartActivity中的if(mCartList.size()== 0)替換if(deletecounter!= 0)條件 –

+0

調用UserActivity時,deletecounter在刪除條件時增加,滿足條件deletecounter!= 0和意圖被激發,以便更好地檢查arraylist的大小,如果0或不0,那麼只有Intent到UserActivity應該被觸發 –

相關問題