|
| 1 | +package com.example.yoga.sqliteexample.Adapter; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.support.v7.widget.RecyclerView; |
| 5 | +import android.view.LayoutInflater; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.LinearLayout; |
| 9 | +import android.widget.TextView; |
| 10 | +import android.widget.Toast; |
| 11 | + |
| 12 | +import com.example.yoga.sqliteexample.Model.Person; |
| 13 | +import com.example.yoga.sqliteexample.R; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by YOGA on 11/15/2016. |
| 20 | + */ |
| 21 | + |
| 22 | +public class BillRecyclerViewAdapter extends RecyclerView.Adapter<BillRecyclerViewAdapter.ViewHolder> { |
| 23 | + private List<String> placeList, dateList; |
| 24 | + private List<Person> payerList; |
| 25 | + private Activity activity; |
| 26 | + |
| 27 | + |
| 28 | + public interface BillRecyclerInterface { |
| 29 | + void setBillDetailFragment(); |
| 30 | + } |
| 31 | + |
| 32 | + public BillRecyclerViewAdapter(Activity activity, List<String> placeList, List<String> dateList, List<Person> payerList) { |
| 33 | + this.activity = activity; |
| 34 | + this.placeList = placeList; |
| 35 | + this.dateList = dateList; |
| 36 | + this.payerList = payerList; |
| 37 | + } |
| 38 | + |
| 39 | + public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { |
| 40 | + public TextView mTitle; |
| 41 | + public TextView textView_line1, textView_line2, textView_line3; |
| 42 | + private Activity activity; |
| 43 | + private BillRecyclerInterface mListener; |
| 44 | + |
| 45 | + public ViewHolder(LinearLayout v, Activity activity) { |
| 46 | + super(v); |
| 47 | + v.setOnClickListener(this); |
| 48 | + |
| 49 | + this.activity = activity; |
| 50 | + try { |
| 51 | + mListener = (BillRecyclerInterface) activity; |
| 52 | + } catch (ClassCastException e) { |
| 53 | + throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener"); |
| 54 | + } |
| 55 | + |
| 56 | + mTitle = (TextView) v.findViewById(R.id.textView_title); |
| 57 | + textView_line1 = (TextView) v.findViewById(R.id.textView_line1); |
| 58 | + textView_line2 = (TextView) v.findViewById(R.id.textView_line2); |
| 59 | + textView_line3 = (TextView) v.findViewById(R.id.textView_line3); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onClick(View v) { |
| 64 | + // Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show(); |
| 65 | + mListener.setBillDetailFragment(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + @Override |
| 72 | + public BillRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 73 | + // create a new view |
| 74 | + View v = LayoutInflater.from(parent.getContext()) |
| 75 | + .inflate(R.layout.listview_layout, parent, false); |
| 76 | + // set the view's size, margins, paddings and layout parameters |
| 77 | + ViewHolder vh = new ViewHolder((LinearLayout) v, activity); |
| 78 | + return vh; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int getItemCount() { |
| 83 | + return placeList.size(); |
| 84 | + } |
| 85 | + |
| 86 | + // Replace the contents of a view (invoked by the layout manager) |
| 87 | + @Override |
| 88 | + public void onBindViewHolder(BillRecyclerViewAdapter.ViewHolder holder, int position) { |
| 89 | + char c = Character.toUpperCase(placeList.get(position).charAt(0)); |
| 90 | + holder.mTitle.setText(String.valueOf(c)); |
| 91 | + holder.textView_line1.setText(placeList.get(position)); |
| 92 | + holder.textView_line2.setText(dateList.get(position)); |
| 93 | + holder.textView_line3.setText(payerList.get(position).getName()); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +} |
0 commit comments