2017-04-06 43 views
-1

您好,我有一個cartFragmentRecyclerView,它由適配器類生成。使用適配器中的數據更改片段中的textview值

在適配器何時創建每個卡片視圖時,計算總值。最後,我想將此總數值傳遞給cartFragment中的tvCartTotalTextview

我能夠通過在cartFragment中創建一個方法並從Adapter調用方法setTvCartTotal()並將總值返回給Fragment來完成此操作。

但我能夠設置TextView的唯一方法是將TextView置於靜態字段中。我知道這會導致內存泄漏,但無法想到更好的解決方案。任何幫助?我還添加了代碼。

謝謝!

片段的購物:

public class ShoppingCartFragment extends Fragment { 

private ArrayList<ShoppingCart> shoppingcartList = new ArrayList<>(); 
private static TextView tvCartTotal; 
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String TAG = "ShoppingFrag"; 

public ShoppingCartFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_shopping_cart, container, false); 
    RecyclerView rvShoppingCart = (RecyclerView)view.findViewById(R.id.rvShoppingCart); 
    rvShoppingCart.setHasFixedSize(true); 
    LinearLayoutManager manager = new LinearLayoutManager(getActivity()); 
    rvShoppingCart.setLayoutManager(manager); 
    ShoppingCartAdapter shoppingCartAdapter = new ShoppingCartAdapter(getActivity()); 
    rvShoppingCart.setAdapter(shoppingCartAdapter); 
    DatabaseHandler db = new DatabaseHandler(getActivity()); 
    shoppingcartList = db.getAllShoppingCart(); 
    shoppingCartAdapter.setshoppingcartList(shoppingcartList); 
    tvCartTotal = (TextView)view.findViewById(R.id.tvCartTotal); 
    return view; 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    shoppingcartList.clear();   
} 

public void setTvCartTotal(String total) { 
    //TextView tvCartTotal = TextView.findViewById(R.id.tvCartTotal); 
    tvCartTotal.setText(total); 
    Log.d("ShoppingCartFrag: ",total); 
} 
} 

適配器:

public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.ShoppingCartViewHolder> { 

private ArrayList<ShoppingCart> shoppingcartList = new ArrayList<>(); 
private LayoutInflater layoutInflater; 
private Context context; 
private double cartTotal=0; 

public ShoppingCartAdapter(Context context){ 
    layoutInflater = LayoutInflater.from(context); 
    this.context = context;   
} 

public void setshoppingcartList(ArrayList<ShoppingCart> newList){ 
    shoppingcartList = new ArrayList<>(); 
    shoppingcartList.addAll(newList); 
    notifyDataSetChanged(); 
} 

@Override 
public ShoppingCartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = layoutInflater.inflate(R.layout.shoppingcart_cardview, parent, false); 
    ShoppingCartViewHolder shoppingCartViewHolder = new ShoppingCartViewHolder(view); 
    return shoppingCartViewHolder; 
} 

@Override 
public void onBindViewHolder(final ShoppingCartViewHolder holder, int position) { 
    final ShoppingCart shoppingCart = shoppingcartList.get(position); 
    holder.tvProductName.setText(shoppingCart.getProductName()); 
    final double price = shoppingCart.getPrice(); 
    holder.tvProductWholesalePrice.setText(Double.toString(price)); 
    final double total = shoppingCart.getTotal(); 
    holder.tvProductTotalSale.setText(Double.toString(total)); 
    final int quantity = shoppingCart.getQuantity(); 
    holder.etProductQuantity.setText(Integer.toString(quantity)); 

    cartTotal = cartTotal + total; 
    ShoppingCartFragment shoppingCartFragment = new ShoppingCartFragment(); 
    shoppingCartFragment.setTvCartTotal("$" + String.valueOf(String.format("%.2f", cartTotal))); 

    TextWatcher watcher = new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      final int q = Integer.valueOf(holder.etProductQuantity.getText().toString()); 
      final double total = price * q; 
      cartTotal = cartTotal - total;     
     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 
      int q = Integer.valueOf(holder.etProductQuantity.getText().toString()); 
      final double total = price * q; 
      holder.tvProductTotalSale.setText(Double.toString(total)); 
      cartTotal = cartTotal + total; 
      ShoppingCartFragment shoppingCartFragment = new ShoppingCartFragment(); 
      shoppingCartFragment.setTvCartTotal("$" + String.valueOf(String.format("%.2f", cartTotal))); 
     } 
    }; 
    holder.etProductQuantity.addTextChangedListener(watcher);     
} 

@Override 
public int getItemCount() { 
    return shoppingcartList.size(); 
} 

static class ShoppingCartViewHolder extends RecyclerView.ViewHolder { 
    private TextView tvProductName; 
    private TextView tvProductWholesalePrice; 
    private TextView tvProductTotalSale; 
    private EditText etProductQuantity; 

    public ShoppingCartViewHolder(View itemView) { 
     super(itemView); 

     tvProductName = (TextView)itemView.findViewById(R.id.tvProductName); 
     tvProductWholesalePrice = (TextView)itemView.findViewById(R.id.tvProductWholesalePrice); 
     tvProductTotalSale = (TextView)itemView.findViewById(R.id.tvProductTotalSale); 
     etProductQuantity = (EditText)itemView.findViewById(R.id.etProductQuantity);    
    } 
} 
} 

回答

0

的車應該在你的應用程序中的單個對象或應存儲在數據庫中。你可以通過這種方式解決很多問題。

然後,視圖(在你的情況下,您的RV中的TextView)將更新Singleton購物車並刷新自己。

否則按照MVP並通過適配器中的演示者。在某些情況下,通過適當的實施可以接受,但這是完全不同的談話。

+0

謝謝您的意見@Chris購物車已經從數據庫中檢索。 總計變化,因爲當查看購物車時,用戶可以更改項目數量,如果發生這種情況TextWatcher更新itemTotal和cartTotal,然後需要更新cartTotal TextView。 cartTotal TextView與RecyclerView分離。 – Kurt

+1

適配器包含您的購物車的最新版本。在您的適配器中創建一個返回cartTotal的方法,並從片段中調用此方法以刷新視圖。您現在需要回答的問題是如何觸發此刷新,我現在有限的時間內可以考慮的一種可能的解決方案是eventbus。 –

+0

只是想讓你知道事件總線做的伎倆不知道它,但我現在喜歡它。 – Kurt

相關問題