如何在其他类中更新Fragment的TextView

时间:2016-12-16 12:20:57

标签: android class textview android-recyclerview

我正在尝试通过单击我的RecyclerView上的按钮来更新Fragment中的TextView。我尝试通过传递Context但是如果我使用Context,我的应用程序在单击按钮时崩溃了。请帮我做。

这是我的RecyclerAdapter

public class CartRecycleAdapter extends RecyclerView.Adapter<CartRecycleAdapter.RecycleViewHolder> {

private ArrayList<CartDataProvider> arraylist = new ArrayList<>();
Context CTX;
public CartRecycleAdapter(ArrayList<CartDataProvider> arraylist, Context ctx){
    this.arraylist = arraylist;
    this.CTX = ctx.getApplicationContext();
}

@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.modelcart, parent,false);
    RecycleViewHolder recycleViewHolder = new RecycleViewHolder(view);
    return recycleViewHolder;
}
@Override
public void onBindViewHolder(final RecycleViewHolder holder, int position) {

    final CartDataProvider homeCycleDataProvider = arraylist.get(position);

    holder.ItemImage.setImageResource(R.drawable.placeholder);
    holder.TXV_ItemName.setText(homeCycleDataProvider.getItemName().toString());
    holder.TXV_PastPrice.setText(String.valueOf(homeCycleDataProvider.getPastPrice()).toString());
    holder.TXV_FixedPrice.setText(String.valueOf(homeCycleDataProvider.getFixedPrice()).toString());
    holder.TXV_Quantity.setText(Integer.valueOf(homeCycleDataProvider.getQuantity()).toString());
    holder.ratingBar.setRating(homeCycleDataProvider.getRating());

    holder.BTN_QuantityPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int x = holder.getAdapterPosition();
            CartDataProvider homeCycleDataProvider = arraylist.get(x);
            int quantity = homeCycleDataProvider.getQuantity();

            homeCycleDataProvider.setQuantity(++quantity);
            holder.TXV_Quantity.setText(String.valueOf(homeCycleDataProvider.getQuantity()));
            TextView textView = (TextView) ((MainActivity)CTX).findViewById(R.id.txt_total_amount);
            textView.setText("ok");
        }
    });
    holder.BTN_QuantityMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int x = holder.getAdapterPosition();
            CartDataProvider homeCycleDataProvider = arraylist.get(x);
            int quantity = homeCycleDataProvider.getQuantity();

            if(quantity != 0){
                homeCycleDataProvider.setQuantity(--quantity);
                holder.TXV_Quantity.setText(String.valueOf(homeCycleDataProvider.getQuantity()));
                TextView textView = (TextView) ((MainActivity)CTX).findViewById(R.id.txt_total_amount);
                textView.setText("ok");

            }

        }
    });
}

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

public static class RecycleViewHolder extends RecyclerView.ViewHolder{

    ImageView ItemImage;
    TextView TXV_ItemName, TXV_PastPrice, TXV_FixedPrice, TXV_Percentage,TXV_Quantity;
    ImageButton BTN_QuantityMinus, BTN_QuantityPlus;
    RatingBar ratingBar;


    public RecycleViewHolder(View view){

        super(view);

        ItemImage = (ImageView) view.findViewById(R.id.item_image);
        TXV_ItemName = (TextView) view.findViewById(R.id.item_name);
        TXV_PastPrice = (TextView) view.findViewById(R.id.past_price);
        TXV_FixedPrice = (TextView) view.findViewById(R.id.fixed_price);
        TXV_Percentage = (TextView) view.findViewById(R.id.percentiage);
        ratingBar = (RatingBar) view.findViewById(R.id.ratingBar);
        TXV_Quantity = (TextView) view.findViewById(R.id.txv_quantity);
        BTN_QuantityMinus = (ImageButton) view.findViewById(R.id.quantity_minus);
        BTN_QuantityPlus = (ImageButton) view.findViewById(R.id.quantity_plus);

    }
}

这是我的Fragment.java

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_cart, container, false);

    TXT_TotalAmout = (TextView) view.findViewById(R.id.txt_total_amount);
    TXT_Quantity = (TextView) view.findViewById(R.id.txv_quantity);

    CartItemRecyclerView = (RecyclerView) view.findViewById(R.id.item_recycler_id);

    int i =0;
    for (String name : itemname){

        CartDataProvider cartDataProvider = new CartDataProvider(itemname[i], imageurl[i],
                rating[i],pastprice[i],fixedprice[i],Quantity);

        arrayList.add(cartDataProvider);

        TotalPrice += fixedprice[i];
        i++;
    }

    adapter = new CartRecycleAdapter(arrayList,getContext());
    CartItemRecyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    CartItemRecyclerView.setLayoutManager(layoutManager);
    CartItemRecyclerView.setAdapter(adapter);
    CartItemRecyclerView.setNestedScrollingEnabled(false);

    return view;
}

1 个答案:

答案 0 :(得分:0)

textview

中将static声明为Fragment Class
public static TextView TXT_TotalAmout, TXT_Quantity;

现在在您的Recycler view

MyFragClass.TXT_TotalAmount.setText("Your Amount");
MyFragClass.TXT_Quantity.setText("Your Amount");