2016-12-16 72 views
-1

我想通過一個按鈕單擊我的RecyclerView更新我的片段內的TextView。我嘗試通過傳遞上下文,但我的應用程序崩潰時單擊按鈕,如果我使用上下文。請幫助我做到這一點。如何更新其他類中的片段的TextView

這裏是我的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; 
} 
+0

與其直接在ViewHolder中更新TextView,更好的模式可能是將一些回調傳遞給您的RecyclerAdapter(例如您的Fragment實現)......這樣,您的RecyclerAdapter就不會與您正在使用的特定片段。 –

回答

0

聲明你textviewstaticFragment Class

public static TextView TXT_TotalAmout, TXT_Quantity; 

現在你Recycler view

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

非常感謝。現在它正在工作..... –

相關問題