99import android .widget .TextView ;
1010import android .widget .Toast ;
1111
12+ import com .example .yoga .sqliteexample .Fragment .ListBillFragment ;
13+ import com .example .yoga .sqliteexample .Model .Bill ;
1214import com .example .yoga .sqliteexample .Model .Person ;
1315import com .example .yoga .sqliteexample .R ;
1416
17+ import java .text .SimpleDateFormat ;
18+ import java .util .ArrayList ;
1519import java .util .List ;
20+ import java .util .Locale ;
1621
1722
1823/**
1924 * Created by YOGA on 11/15/2016.
2025 */
2126
2227public class BillRecyclerViewAdapter extends RecyclerView .Adapter <BillRecyclerViewAdapter .ViewHolder > {
28+ private List <Bill > billList ;
2329 private List <String > placeList , dateList ;
2430 private List <Person > payerList ;
2531 private Activity activity ;
2632
2733
2834 public interface BillRecyclerInterface {
29- void setBillDetailFragment ();
35+ void setBillDetailFragment (Bill bill , Person payer );
3036 }
3137
32- public BillRecyclerViewAdapter (Activity activity , List <String > placeList , List < String > dateList , List <Person > payerList ) {
38+ public BillRecyclerViewAdapter (Activity activity , List <Bill > billList , List <Person > payerList ) {
3339 this .activity = activity ;
34- this .placeList = placeList ;
35- this .dateList = dateList ;
40+ this .billList = billList ;
3641 this .payerList = payerList ;
42+
43+ placeList = new ArrayList <>();
44+ dateList = new ArrayList <>();
45+
46+ SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm" , Locale .getDefault ());
47+
48+ for (int i = 0 ; i < billList .size (); ++i ) {
49+ Bill b = billList .get (i );
50+ placeList .add (b .getPlace ());
51+ dateList .add (dateFormat .format (b .getDate ()));
52+ }
3753 }
3854
3955 public static class ViewHolder extends RecyclerView .ViewHolder implements View .OnClickListener {
4056 public TextView mTitle ;
4157 public TextView textView_line1 , textView_line2 , textView_line3 ;
58+ private List <Bill > billList ;
59+ private List <Person > payerList ;
4260 private Activity activity ;
4361 private BillRecyclerInterface mListener ;
4462
45- public ViewHolder (LinearLayout v , Activity activity ) {
63+
64+ public ViewHolder (LinearLayout v , Activity activity , List <Bill > billList , List <Person > payerList ) {
4665 super (v );
4766 v .setOnClickListener (this );
4867
4968 this .activity = activity ;
69+ this .billList = billList ;
70+ this .payerList = payerList ;
5071 try {
5172 mListener = (BillRecyclerInterface ) activity ;
5273 } catch (ClassCastException e ) {
@@ -62,7 +83,7 @@ public ViewHolder(LinearLayout v, Activity activity) {
6283 @ Override
6384 public void onClick (View v ) {
6485 // Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show();
65- mListener .setBillDetailFragment ();
86+ mListener .setBillDetailFragment (billList . get ( getAdapterPosition ()), payerList . get ( getAdapterPosition ()) );
6687 }
6788 }
6889
@@ -74,24 +95,23 @@ public BillRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, i
7495 View v = LayoutInflater .from (parent .getContext ())
7596 .inflate (R .layout .listview_layout , parent , false );
7697 // set the view's size, margins, paddings and layout parameters
77- ViewHolder vh = new ViewHolder ((LinearLayout ) v , activity );
98+ ViewHolder vh = new ViewHolder ((LinearLayout ) v , activity , billList , payerList );
7899 return vh ;
79100 }
80101
81102 @ Override
82103 public int getItemCount () {
83- return placeList .size ();
104+ return billList .size ();
84105 }
85106
86107 // Replace the contents of a view (invoked by the layout manager)
87108 @ Override
88109 public void onBindViewHolder (BillRecyclerViewAdapter .ViewHolder holder , int position ) {
89110 char c = Character .toUpperCase (placeList .get (position ).charAt (0 ));
90111 holder .mTitle .setText (String .valueOf (c ));
112+
91113 holder .textView_line1 .setText (placeList .get (position ));
92114 holder .textView_line2 .setText (dateList .get (position ));
93115 holder .textView_line3 .setText (payerList .get (position ).getName ());
94116 }
95-
96-
97117}
0 commit comments