Hallo Ich habe eine cartFragment
mit RecyclerView
, die von einer Adapterklasse generiert wird.ändern Textansicht Wert in Fragment mit Daten von Adapter
Im Adapter wird beim Erstellen jeder Kartenansicht ein Gesamtwert berechnet. Am Ende möchte ich diesen Gesamtwert an einen tvCartTotal
im cartFragment
übergeben.
Ich konnte dies erreichen, indem Sie eine Methode in cartFragment
erstellen und die Methode setTvCartTotal()
vom Adapter aufrufen und den Gesamtwert an das Fragment zurückgeben.
Aber die einzige Möglichkeit, die ich setzen konnte TextView
war, indem Sie die TextView
im statischen Feld. Ich weiß, dass dies Speicherlecks verursacht, aber ich kann mir keine bessere Lösung vorstellen. Irgendeine Hilfe? Ich habe auch den Code hinzugefügt.
Danke!
ShoppingCart Fragment:
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);
}
}
Adapter:
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);
}
}
}
Vielen Dank für die Kommentare @Chris der Warenkorb wird bereits aus einer Datenbank abgerufen. Der Gesamtwert ändert sich, weil der Benutzer beim Anzeigen des Einkaufswagens die Artikelmengen ändern kann. Wenn dies der Fall ist, aktualisiert der TextWatcher den Artikelinhalt und den Einkaufswageninhalt, die dann die Einkaufswagengesamtansicht aktualisieren müssen. Die cartTotal TextView ist von der RecyclerView getrennt. – Kurt
Der Adapter enthält die neueste Version Ihres Einkaufswagens. Erstellen Sie in Ihrem Adapter eine Methode, die den Parameter cartTotal zurückgibt, und rufen Sie diese Methode aus Ihrem Fragment auf, um Ihre Ansicht zu aktualisieren. Die Frage, die Sie jetzt beantworten müssen, ist, wie Sie diese Aktualisierung auslösen können. Eine mögliche Lösung, die ich in der begrenzten Zeit, die ich jetzt habe, denken kann, ist Eventbus. –
Ich wollte nur wissen lassen, dass eventbus den Trick nicht wusste, aber ich liebe es jetzt. – Kurt