2017-04-06 44 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

相关问题