2017-01-02 11 views
-3

recyclerviewのTextview以下の緑の値の合計に合計を設定します。ここに私のアプリの断片のスクリーンショットがあります。TextViewの値を、recyclerviewアダプタの外にあるrecyclerviewアイテムの値の合計に設定するには

これは、これが

パブリッククラスInvoiceDetailFragmentフラグメント{

RecyclerView invoicedproducts; 
Button showTransfered; 
InvoiceAdapter invoiceAdapter; 
TextView subTotal; 
List<ProductVariables> mytotal; 
ProductVariables calculatetotal; 


public InvoiceDetailFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_invoice_detail, container, false); 


    subTotal = (TextView) view.findViewById(R.id.subTotal); 
    showTransfered = (Button) view.findViewById(R.id.showTransfered); 
    invoicedproducts = (RecyclerView) view.findViewById(R.id.invoicedproducts); 

    showTransfered.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      GetTotal(v); 
     } 
    }); 


    return view; 
} 

public void GetTotal(View view) { 


} 







public void SetMessage(List<ProductVariables> productlists){ 
    List<List<ProductVariables>> ite2 = new ArrayList<>(); 
    ite2.add(productlists); 
    invoicedproducts.setLayoutManager(new LinearLayoutManager(getContext())); 
    InvoiceAdapter adapter = new InvoiceAdapter(getContext(), productlists); 
    invoicedproducts.setAdapter(adapter); 
を拡張私の断片クラスである

class InvoiceAdapter extends RecyclerView.Adapter<InvoiceAdapter.MyViewHolder>{ 
 

 
    Context mcontext; 
 
    List<ProductVariables> productlist; 
 
    ProductVariables variables; 
 
    List<Double> mytota = new ArrayList<>(); 
 

 

 
    public InvoiceAdapter(Context mcontext, List<ProductVariables> productlist) { 
 
     this.mcontext = mcontext; 
 
     this.productlist = productlist; 
 
    } 
 

 
    public class MyViewHolder extends RecyclerView.ViewHolder{ 
 

 
     TextView producttransfered; 
 
     TextView rateofselectedproduct; 
 
     EditText enterqty; 
 
     TextView totalitemamt; 
 
     TextView subTotal; 
 

 
     public MyViewHolder(View itemView) { 
 
      super(itemView); 
 

 
      producttransfered = (TextView) itemView.findViewById(R.id.producttransfered); 
 
      rateofselectedproduct = (TextView) itemView.findViewById(R.id.rateofselectedproduct); 
 
      totalitemamt = (TextView) itemView.findViewById(R.id.totalitemamt); 
 
      subTotal = (TextView) itemView.findViewById(R.id.subTotal); 
 
      enterqty = (EditText) itemView.findViewById(R.id.enterqty); 
 

 

 
     } 
 
    } 
 

 
    @Override 
 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
 
     Context context = parent.getContext(); 
 
     LayoutInflater inflater = LayoutInflater.from(context); 
 
     View invoiceview = inflater.inflate(R.layout.custominvoicedetail, parent, false); 
 
     MyViewHolder myViewHolder = new MyViewHolder(invoiceview); 
 
     return myViewHolder; 
 
    } 
 

 
    @Override 
 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 
 
     variables = productlist.get(position); 
 

 
     TextView transfereditemname = holder.producttransfered; 
 
     transfereditemname.setText(variables.getProductname()); 
 

 
     TextView rates = holder.rateofselectedproduct; 
 
     rates.setText(Double.toString(variables.getRate())); 
 

 

 

 
     holder.enterqty.addTextChangedListener(new TextWatcher() { 
 
      @Override 
 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
 

 
      } 
 

 
      @Override 
 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
 
       String qty = s.toString(); 
 

 
       try { 
 
        double input = Double.parseDouble(qty); 
 
        Double rate = Double.parseDouble(holder.rateofselectedproduct.getText().toString()); 
 
        Double total = rate * input; 
 

 
        holder.totalitemamt.setText(Double.toString(total)); 
 

 

 
       } catch (NumberFormatException e) { 
 
        holder.totalitemamt.setText(""); 
 
       } catch (Exception e) { 
 
        holder.totalitemamt.setText(""); 
 
       } 
 

 
      } 
 

 
      @Override 
 
      public void afterTextChanged(Editable s) { 
 

 
      } 
 
     }); 
 

 

 

 

 

 
    } 
 

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

 

 

 

 
}

私のアダプタクラスであります0

強いテキスト }

答えて

-1

つの方法は、あなたがthis-

  1. あなたはあなたのrecyclerviewアダプタ外の合計の値を更新するためのインタフェースを行うことができます行うことができます。

  2. パラメータで整数値をとり、totalを設定するフラグメントクラス内のメソッドを作成します。アダプタコンストラクタにフラグメントオブジェクトを渡します。 そのフラグメントオブジェクトを使用して、フラグメントで作成したメソッドを呼び出し、値を渡します。

私はあなたに役立つことを願っています。あなたのフラグメントクラスで

 public class InvoiceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    Context mcontext; 
     List<ProductVariables> productlist; 
     ProductVariables variables; 
     List<Double> mytota = new ArrayList<>(); 
     private SubTotalListener subTotalListener; 


     public InvoiceAdapter(Context mcontext, List<ProductVariables> productlist, SubTotalListener subTotalListener) { 
    this.subTotalListener = subTotalListener; 
      this.mcontext = mcontext; 
      this.productlist = productlist; 
     } 

     public class MyViewHolder extends RecyclerView.ViewHolder{ 

      TextView producttransfered; 
      TextView rateofselectedproduct; 
      EditText enterqty; 
      TextView totalitemamt; 
      TextView subTotal; 

      public MyViewHolder(View itemView) { 
       super(itemView); 

       producttransfered = (TextView) itemView.findViewById(R.id.producttransfered); 
       rateofselectedproduct = (TextView) itemView.findViewById(R.id.rateofselectedproduct); 
       totalitemamt = (TextView) itemView.findViewById(R.id.totalitemamt); 
       subTotal = (TextView) itemView.findViewById(R.id.subTotal); 
       enterqty = (EditText) itemView.findViewById(R.id.enterqty); 


      } 
     } 

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

      Context context = parent.getContext(); 
      LayoutInflater inflater = LayoutInflater.from(context); 
      View invoiceview = inflater.inflate(R.layout.custominvoicedetail, parent, false); 
      MyViewHolder myViewHolder = new MyViewHolder(invoiceview); 
      return myViewHolder; 
     } 

     @Override 
     public void onBindViewHolder(final MyViewHolder holder, final int position) { 
      variables = productlist.get(position); 

      TextView transfereditemname = holder.producttransfered; 
      transfereditemname.setText(variables.getProductname()); 

      TextView rates = holder.rateofselectedproduct; 
      rates.setText(Double.toString(variables.getRate())); 



      holder.enterqty.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

       } 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        String qty = s.toString(); 

        try { 
         double input = Double.parseDouble(qty); 
         Double rate = Double.parseDouble(holder.rateofselectedproduct.getText().toString()); 
         Double total = rate * input; 

         holder.totalitemamt.setText(Double.toString(total)); 


        } catch (NumberFormatException e) { 
         holder.totalitemamt.setText(""); 
        } catch (Exception e) { 
         holder.totalitemamt.setText(""); 
        } 

       } 

       @Override 
       public void afterTextChanged(Editable s) { 

       } 
      }); 
     } 

public void countTotal(){ 
// Calculate your total amount here from your arraylist and pass in below call. 
subTotalListener.onSubTotalUpdate(total); 
} 


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

    public interface SubTotalListener { 
        void onSubTotalUpdate(int total); 
     } 

、私は私のアダプタでメソッドを作成するときにbelow-

public class InvoiceDetailFragment extends Fragment implements InvoiceAdapter.SubTotalListener { 

RecyclerView invoicedproducts; 
Button showTransfered; 
InvoiceAdapter invoiceAdapter; 
TextView subTotal; 
List<ProductVariables> mytotal; 
ProductVariables calculatetotal; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_invoice_detail, container, false); 


    subTotal = (TextView) view.findViewById(R.id.subTotal); 
    showTransfered = (Button) view.findViewById(R.id.showTransfered); 
    invoicedproducts = (RecyclerView) view.findViewById(R.id.invoicedproducts); 

invoiceAdapter = new InvoiceAdapter(this,mytotal,this); 
    return view; 
} 

@Override 
void onSubTotalUpdate(int total){ 
subTotal.setText(""+total); 
} 
+0

のようにしてくださいと私はどんなことなく、私の完全なコードをアップロードしたnullポインタ – Vernon

+0

を取得し、私のフラグメントにそのメソッドを呼び出しますインターフェイスまたはメソッド..テキストビューの緑の値はedittextウォッチャー経由で生成されますが、フラグメントの合計を示すテキストビューでそのアダプタの外に合計値を生成する必要があります。あなたが提案したように、私はメソッドを作成し、y arraylistを作成しましたが、nullpointerエラーが発生しました – Vernon

+0

私の投稿を編集しました。希望それはあなたを助けます。 –

関連する問題