Skip to content

Commit cbe1eab

Browse files
committed
Make Bill Details Fragment to work
1 parent 9c16d72 commit cbe1eab

7 files changed

Lines changed: 156 additions & 44 deletions

File tree

‎app/src/main/java/com/example/yoga/sqliteexample/Adapter/BillRecyclerViewAdapter.java‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,65 @@
99
import android.widget.TextView;
1010
import android.widget.Toast;
1111

12+
import com.example.yoga.sqliteexample.Fragment.ListBillFragment;
13+
import com.example.yoga.sqliteexample.Model.Bill;
1214
import com.example.yoga.sqliteexample.Model.Person;
1315
import com.example.yoga.sqliteexample.R;
1416

17+
import java.text.SimpleDateFormat;
18+
import java.util.ArrayList;
1519
import java.util.List;
20+
import java.util.Locale;
1621

1722

1823
/**
1924
* Created by YOGA on 11/15/2016.
2025
*/
2126

2227
public 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
}

‎app/src/main/java/com/example/yoga/sqliteexample/Fragment/BillDetailFragment.java‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,58 @@
1212
import android.view.MenuItem;
1313
import android.view.View;
1414
import android.view.ViewGroup;
15+
import android.widget.TextView;
1516

1617
import com.example.yoga.sqliteexample.MainPage;
18+
import com.example.yoga.sqliteexample.Model.Bill;
19+
import com.example.yoga.sqliteexample.Model.Person;
1720
import com.example.yoga.sqliteexample.R;
1821

22+
import java.text.SimpleDateFormat;
23+
import java.util.Locale;
24+
1925
/**
2026
* Created by YOGA on 11/19/2016.
2127
*/
2228

2329
public class BillDetailFragment extends Fragment {
2430
public static final String TAG = "BillDetailFragment";
31+
private TextView bill_detail_textView_place, bill_detail_textView_date, bill_detail_textView_payer;
32+
private Bill bill;
33+
private Person payer;
34+
35+
public void setBill(Bill bill) {
36+
this.bill = bill;
37+
}
38+
39+
public void setPayer(Person payer) {
40+
this.payer = payer;
41+
}
2542

2643
@Override
2744
public void onAttach(Context context) {
2845
super.onAttach(context);
2946
}
3047

48+
49+
@Override
50+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
51+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
52+
53+
View myView = inflater.inflate(R.layout.fragment_bill_detail, container, false);
54+
bill_detail_textView_place = (TextView) myView.findViewById(R.id.bill_detail_textView_place);
55+
bill_detail_textView_date = (TextView) myView.findViewById(R.id.bill_detail_textView_date);
56+
bill_detail_textView_payer = (TextView) myView.findViewById(R.id.bill_detail_textView_payer);
57+
58+
bill_detail_textView_place.setText(bill.getPlace());
59+
bill_detail_textView_date.setText(dateFormat.format(bill.getDate()));
60+
bill_detail_textView_payer.setText(payer.getName());
61+
62+
63+
printFragmentStackName();
64+
return myView;
65+
}
66+
3167
private void printFragmentStackName() {
3268
FragmentManager fm = getFragmentManager();
3369
Log.i(TAG, "Finding fragments.");
@@ -38,11 +74,4 @@ private void printFragmentStackName() {
3874
}
3975
}
4076

41-
@Override
42-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
43-
View myView = inflater.inflate(R.layout.fragment_bill_detail, container, false);
44-
printFragmentStackName();
45-
return myView;
46-
}
47-
4877
}

‎app/src/main/java/com/example/yoga/sqliteexample/Fragment/ListBillFragment.java‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public interface ListBillInterface {
3535
Person getPerson(long person_id);
3636
}
3737

38-
3938
ListBillInterface mListener;
39+
4040
@Override
4141
public void onAttach(Context context) {
4242
super.onAttach(context);
@@ -63,23 +63,15 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
6363

6464
drawVerticalLines(myView);
6565

66-
// specify an adapter (see also next example)
66+
List<Person> personList = new ArrayList<>();
6767
List<Bill> billList = mListener.getAllBills();
68-
List<String> placeList = new ArrayList<>();
69-
List<String> dateList = new ArrayList<>();
70-
List<Person> payerList = new ArrayList<>();
71-
72-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
73-
74-
for (int i = 0; i < billList.size(); ++i) {
75-
Bill b = billList.get(i);
76-
Person p = mListener.getPerson(b.getPayer());
77-
placeList.add(b.getPlace());
78-
dateList.add(dateFormat.format(b.getDate()));
79-
payerList.add(p);
68+
69+
for (Bill b: billList) {
70+
personList.add(mListener.getPerson(b.getPayer()));
8071
}
8172

82-
mAdapter = new BillRecyclerViewAdapter(getActivity(), placeList, dateList, payerList);
73+
// specify an adapter
74+
mAdapter = new BillRecyclerViewAdapter(getActivity(), billList, personList);
8375
mRecyclerView.setAdapter(mAdapter);
8476

8577
return myView;
@@ -99,5 +91,4 @@ public void drawVerticalLines(View view) {
9991
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));
10092
}
10193

102-
10394
}

‎app/src/main/java/com/example/yoga/sqliteexample/MainPage.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public void displayView(int viewId) {
177177
}
178178

179179
if (fragment != null) {
180+
getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
180181
FragmentTransaction ft = getFragmentManager().beginTransaction();
181182
ft.replace(R.id.content_frame, fragment);
182183
ft.commit();
@@ -221,8 +222,10 @@ public long createItem(Item item) {
221222
}
222223

223224
@Override
224-
public void setBillDetailFragment() {
225+
public void setBillDetailFragment(Bill bill, Person payer) {
225226
BillDetailFragment billDetailFragment = new BillDetailFragment();
227+
billDetailFragment.setBill(bill);
228+
billDetailFragment.setPayer(payer);
226229
FragmentTransaction ft = getFragmentManager().beginTransaction();
227230
ft.replace(R.id.content_frame, billDetailFragment);
228231
ft.addToBackStack("List Bill");
@@ -235,6 +238,7 @@ public void setBillDetailFragment() {
235238
private void setAddBillFragment() {
236239
AddBillFragment addBillFragment = new AddBillFragment();
237240
FragmentTransaction ft = getFragmentManager().beginTransaction();
241+
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
238242
ft.replace(R.id.content_frame, addBillFragment);
239243
ft.addToBackStack("List Bill");
240244
ft.commit();

‎app/src/main/java/com/example/yoga/sqliteexample/Model/myDatabaseHelper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public List<Person> getAllPeople() {
210210
List<Person> p = new ArrayList<>();
211211
String selectQuery = "SELECT * FROM " + Person.PersonEntry.TABLE_NAME;
212212

213-
Log.e(LOG, selectQuery);
213+
Log.d(LOG, selectQuery);
214214

215215
SQLiteDatabase db = this.getReadableDatabase();
216216
Cursor c = db.rawQuery(selectQuery, null);
Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,80 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
4-
android:layout_height="match_parent">
4+
android:layout_height="match_parent"
5+
android:paddingBottom="@dimen/activity_vertical_margin"
6+
android:paddingLeft="@dimen/activity_horizontal_margin"
7+
android:paddingRight="@dimen/activity_horizontal_margin"
8+
android:paddingTop="@dimen/activity_vertical_margin"
9+
android:orientation="vertical">
510

6-
<TextView
7-
android:text="Bill Detail"
8-
android:layout_width="wrap_content"
11+
<LinearLayout
12+
android:layout_width="match_parent"
913
android:layout_height="wrap_content"
10-
android:id="@+id/textView2"
11-
android:layout_weight="1" />
14+
android:orientation="horizontal">
15+
<TextView
16+
android:text="Place: "
17+
android:layout_width="80sp"
18+
android:layout_height="wrap_content"
19+
android:id="@+id/textView3"
20+
android:textSize="16sp" />
21+
<TextView
22+
android:text="Place"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:textSize="16sp"
26+
android:id="@+id/bill_detail_textView_place" />
27+
</LinearLayout>
28+
29+
<LinearLayout
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:orientation="horizontal">
33+
<TextView
34+
android:text="Date: "
35+
android:layout_width="80sp"
36+
android:layout_height="wrap_content"
37+
android:textSize="16sp"
38+
android:id="@+id/textView2" />
39+
<TextView
40+
android:text="Date"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:textSize="16sp"
44+
android:id="@+id/bill_detail_textView_date" />
45+
</LinearLayout>
46+
47+
<LinearLayout
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
android:orientation="horizontal">
51+
<TextView
52+
android:text="Payer: "
53+
android:layout_width="80sp"
54+
android:layout_height="wrap_content"
55+
android:textSize="16sp"
56+
android:id="@+id/textView4" />
57+
<TextView
58+
android:text="Payer"
59+
android:layout_width="wrap_content"
60+
android:layout_height="wrap_content"
61+
android:textSize="16sp"
62+
android:id="@+id/bill_detail_textView_payer" />
63+
</LinearLayout>
64+
65+
66+
67+
<android.support.v7.widget.RecyclerView
68+
android:layout_width="match_parent"
69+
android:layout_height="0dp"
70+
android:layout_weight="1"
71+
android:id="@+id/bill_detail_recyclerView">
72+
73+
</android.support.v7.widget.RecyclerView>
74+
75+
<Button
76+
android:text="Add Item"
77+
android:layout_width="match_parent"
78+
android:layout_height="wrap_content"
79+
android:id="@+id/bill_detail_button" />
1280
</LinearLayout>

‎app/src/main/res/layout/listview_layout.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727
android:id="@+id/textView_line1"
2828
android:layout_width="match_parent"
2929
android:layout_height="wrap_content"
30-
android:textSize="18sp"
30+
android:textSize="16sp"
3131
android:padding="0sp"
3232
android:text="Place" />
3333
<TextView
3434
android:id="@+id/textView_line2"
3535
android:layout_width="match_parent"
3636
android:layout_height="wrap_content"
37-
android:textSize="18sp"
37+
android:textSize="16sp"
3838
android:padding="0sp"
3939
android:text="Date" />
4040
<TextView
4141
android:id="@+id/textView_line3"
4242
android:layout_width="match_parent"
4343
android:layout_height="wrap_content"
44-
android:textSize="18sp"
44+
android:textSize="16sp"
4545
android:padding="0sp"
4646
android:text="Payer" />
4747
</LinearLayout>

0 commit comments

Comments
 (0)