使用适配器中的数据更改片段中的textview值

时间:2017-04-06 23:06:20

标签: android android-fragments android-adapter

您好我有cartFragment RecyclerView由适配器类生成。

在适配器中,当创建每个卡片视图时,将计算总值。最后,我想将此总价值传递给tvCartTotal中的Textview cartFragment

我能够通过在cartFragment中创建方法并从适配器调用方法setTvCartTotal()并将总值返回到片段来完成此操作。

但我能够设置TextView的唯一方法是将TextView放在静态字段中。我知道这会导致内存泄漏,但无法想到更好的解决方案。任何帮助?我还添加了代码。

谢谢!

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);
}
}

适配器:

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);            
    }
}
}

1 个答案:

答案 0 :(得分:0)

购物车应该是应用程序中的单个对象,或者应该存储在数据库中。你可以通过这种方式解决很多问题。

然后,视图(在您的情况下,RV中的TextView)将更新Singleton Cart并自行刷新。

否则请按照MVP并在适配器中传递演示者。在某些情况下并且通过适当的实施可以接受,但这是一个完全不同的对话。