Skip to content

Commit 47402ee

Browse files
committed
Minor update
Creating edit page for bill details Added edit bill function for database
1 parent 3ba39d8 commit 47402ee

5 files changed

Lines changed: 137 additions & 34 deletions

File tree

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,28 @@
3535
* Created by YOGA on 11/6/2016.
3636
*/
3737
public class AddBillFragment extends Fragment implements OnDateSetListener, TimePickerDialog.OnTimeSetListener {
38-
Spinner spinner_add_bill;
39-
Button button_add_bill_pickdate, button_add_bill_add, button_add_bill_picktime;
40-
EditText editText_add_bill;
38+
private Spinner spinner_add_bill;
39+
private Button button_add_bill_pickdate, button_add_bill_add, button_add_bill_picktime;
40+
private EditText editText_add_bill;
4141
private Date date = null, time = null;
42-
List<Person> personList;
43-
int payer_id = -1;
42+
private List<Person> personList;
43+
private int payer_id = -1, bill_id = -1;
44+
45+
public void setBill(Bill bill, Person payer) {
46+
this.date = bill.getDate();
47+
this.time = bill.getDate();
48+
setPickDate(date);
49+
setPickTime(time);
50+
button_add_bill_add.setText("Edit Bill");
51+
editText_add_bill.setText(bill.getPlace());
52+
spinner_add_bill.setSelection(personList.indexOf(payer));
53+
bill_id = bill.getId();
54+
}
4455

4556
public interface AddBillInterface {
4657
List<Person> getAllPeople();
4758
long createBill(Bill bill);
59+
long editBill(Bill bill);
4860
}
4961

5062
AddBillInterface mListener;
@@ -134,10 +146,8 @@ public void onClick(View v) {
134146
@Override
135147
public void onClick(View v) {
136148
// Check if no view has focus:
137-
if (myView != null) {
138-
InputMethodManager imm = (InputMethodManager) myView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
139-
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
140-
}
149+
InputMethodManager imm = (InputMethodManager) myView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
150+
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
141151

142152
if (date == null || time == null || payer_id == -1 || editText_add_bill.getText().toString().equals("")) {
143153
Toast.makeText(myView.getContext(), "Please select date and payer.", Toast.LENGTH_SHORT).show();
@@ -149,32 +159,51 @@ public void onClick(View v) {
149159
date.setSeconds(time.getSeconds());
150160
bill.setDate(date);
151161
bill.setPayer(payer_id);
152-
if(mListener.createBill(bill) == -1) {
153-
Toast.makeText(myView.getContext(), "Error occurred when inserting into database.", Toast.LENGTH_SHORT).show();
162+
bill.setId(bill_id);
163+
164+
if (bill_id == -1) {
165+
if(mListener.createBill(bill) == -1) {
166+
Toast.makeText(myView.getContext(), "Error occurred when inserting into database.", Toast.LENGTH_SHORT).show();
167+
} else {
168+
Toast.makeText(myView.getContext(), "Bill added.", Toast.LENGTH_SHORT).show();
169+
}
154170
} else {
155-
Toast.makeText(myView.getContext(), "Bill added.", Toast.LENGTH_SHORT).show();
171+
if(mListener.editBill(bill) == -1) {
172+
Toast.makeText(myView.getContext(), "Error occurred when inserting into database.", Toast.LENGTH_SHORT).show();
173+
} else {
174+
Toast.makeText(myView.getContext(), "Bill edited.", Toast.LENGTH_SHORT).show();
175+
}
156176
}
157-
158177
}
159178
}
160179
});
161180

162181
return myView;
163182
}
164183

184+
private void setPickDate(Date date) {
185+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
186+
button_add_bill_pickdate.setText(dateFormat.format(date));
187+
}
188+
189+
private void setPickTime(Date time) {
190+
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
191+
button_add_bill_picktime.setText(dateFormat.format(time));
192+
}
193+
165194
@Override
166195
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
167196
Calendar c = new GregorianCalendar(year, monthOfYear, dayOfMonth);
168197
date = c.getTime();
169-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
170-
button_add_bill_pickdate.setText(dateFormat.format(date));
198+
setPickDate(date);
171199
}
172200

173201
@Override
174202
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
175203
Calendar c = new GregorianCalendar(0, 0, 0, hourOfDay, minute, second);
176204
time = c.getTime();
177-
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
178-
button_add_bill_picktime.setText(dateFormat.format(time));
205+
setPickTime(time);
179206
}
207+
208+
180209
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.yoga.sqliteexample.Fragment;
22

33

4+
import android.app.Activity;
45
import android.app.Fragment;
56
import android.app.FragmentManager;
67
import android.content.Context;
@@ -12,6 +13,7 @@
1213
import android.view.MenuItem;
1314
import android.view.View;
1415
import android.view.ViewGroup;
16+
import android.widget.Button;
1517
import android.widget.TextView;
1618

1719
import com.example.yoga.sqliteexample.MainPage;
@@ -29,13 +31,17 @@
2931
public class BillDetailFragment extends Fragment {
3032
public static final String TAG = "BillDetailFragment";
3133
private TextView bill_detail_textView_place, bill_detail_textView_date, bill_detail_textView_payer;
34+
private Button bill_detail_add_button, bill_detail_edit_button;
3235
private Bill bill;
3336
private Person payer;
3437

38+
public interface BillDetailFragmentInterface {
39+
void setEditBillFragment(Bill bill, Person payer);
40+
}
41+
3542
public void setBill(Bill bill) {
3643
this.bill = bill;
3744
}
38-
3945
public void setPayer(Person payer) {
4046
this.payer = payer;
4147
}
@@ -54,16 +60,34 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5460
bill_detail_textView_place = (TextView) myView.findViewById(R.id.bill_detail_textView_place);
5561
bill_detail_textView_date = (TextView) myView.findViewById(R.id.bill_detail_textView_date);
5662
bill_detail_textView_payer = (TextView) myView.findViewById(R.id.bill_detail_textView_payer);
63+
bill_detail_add_button = (Button) myView.findViewById(R.id.bill_detail_add_button);
64+
bill_detail_edit_button = (Button) myView.findViewById(R.id.bill_detail_edit_button);
65+
66+
bill_detail_edit_button.setOnClickListener(new View.OnClickListener() {
67+
@Override
68+
public void onClick(View v) {
69+
70+
71+
Activity activity = getActivity();
72+
try {
73+
BillDetailFragmentInterface mListener = (BillDetailFragmentInterface) activity;
74+
mListener.setEditBillFragment(bill, payer);
75+
} catch (ClassCastException e) {
76+
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
77+
}
78+
}
79+
});
5780

5881
bill_detail_textView_place.setText(bill.getPlace());
5982
bill_detail_textView_date.setText(dateFormat.format(bill.getDate()));
6083
bill_detail_textView_payer.setText(payer.getName());
6184

62-
6385
printFragmentStackName();
6486
return myView;
6587
}
6688

89+
90+
6791
private void printFragmentStackName() {
6892
FragmentManager fm = getFragmentManager();
6993
Log.i(TAG, "Finding fragments.");

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public class MainPage extends AppCompatActivity
2727
ListPersonFragment.ListPersonInterface,
2828
AddBillFragment.AddBillInterface,
2929
ListBillFragment.ListBillInterface,
30-
BillRecyclerViewAdapter.BillRecyclerInterface {
30+
BillRecyclerViewAdapter.BillRecyclerInterface,
31+
BillDetailFragment.BillDetailFragmentInterface {
3132

3233
private static final String TAG = "MainPage";
3334
// Database Helper
@@ -211,6 +212,11 @@ public long createBill(Bill bill) {
211212
return db.createBill(bill);
212213
}
213214

215+
@Override
216+
public long editBill(Bill bill) {
217+
return db.editBill(bill);
218+
}
219+
214220
@Override
215221
public List<Bill> getAllBills() {
216222
return db.getAllBills();
@@ -235,6 +241,21 @@ public void setBillDetailFragment(Bill bill, Person payer) {
235241
displayHomeIcon(false);
236242
}
237243

244+
@Override
245+
public void setEditBillFragment(Bill bill, Person payer) {
246+
AddBillFragment addBillFragment = new AddBillFragment();
247+
FragmentTransaction ft = getFragmentManager().beginTransaction();
248+
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
249+
ft.replace(R.id.content_frame, addBillFragment);
250+
ft.addToBackStack("Bill Details");
251+
ft.commit();
252+
getFragmentManager().executePendingTransactions();
253+
254+
actionBar.setTitle("Edit Bill");
255+
displayHomeIcon(false);
256+
addBillFragment.setBill(bill, payer);
257+
}
258+
238259
private void setAddBillFragment() {
239260
AddBillFragment addBillFragment = new AddBillFragment();
240261
FragmentTransaction ft = getFragmentManager().beginTransaction();

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class myDatabaseHelper extends SQLiteOpenHelper {
3838
// If you change the database schema, you must increment the database version.
3939
public static final int DATABASE_VERSION = 1;
4040
public static final String DATABASE_NAME = "MyDatabase.db";
41-
41+
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
4242

4343
/*
4444
CREATE TABLE person_table (
@@ -252,8 +252,6 @@ public Person getPerson(long person_id) {
252252
public long createBill(Bill bill) {
253253
SQLiteDatabase db = this.getWritableDatabase();
254254

255-
SimpleDateFormat dateFormat = new SimpleDateFormat(
256-
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
257255

258256
ContentValues values = new ContentValues();
259257
values.put(Bill.BillEntry.COLUMN_NAME_DATE, dateFormat.format(bill.getDate()));
@@ -263,6 +261,19 @@ public long createBill(Bill bill) {
263261
return db.insert(Bill.BillEntry.TABLE_NAME, null, values);
264262
}
265263

264+
public long editBill(Bill bill) {
265+
Log.d(LOG, "editBill with id = " + String.valueOf(bill.getId()));
266+
SQLiteDatabase db = this.getWritableDatabase();
267+
268+
ContentValues values = new ContentValues();
269+
values.put(Bill.BillEntry.COLUMN_NAME_DATE, dateFormat.format(bill.getDate()));
270+
values.put(Bill.BillEntry.COLUMN_NAME_PLACE, bill.getPlace());
271+
values.put(Bill.BillEntry.COLUMN_NAME_PAYER, bill.getPayer());
272+
273+
String strFilter = Bill.BillEntry._ID + " = " + String.valueOf(bill.getId());
274+
return db.update(Bill.BillEntry.TABLE_NAME, values, strFilter, null);
275+
}
276+
266277
public List<Bill> getAllBills() {
267278
List<Bill> billList = new ArrayList<>();
268279
String selectQuery = "SELECT * FROM " + Bill.BillEntry.TABLE_NAME;
@@ -279,10 +290,9 @@ public List<Bill> getAllBills() {
279290
b.setPayer(c.getInt(c.getColumnIndex(Bill.BillEntry.COLUMN_NAME_PAYER)));
280291
b.setPlace(c.getString(c.getColumnIndex(Bill.BillEntry.COLUMN_NAME_PLACE)));
281292

282-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault());
283293
Date dt; //replace 4 with the column index
284294
try {
285-
dt = sdf.parse(c.getString(c.getColumnIndex(Bill.BillEntry.COLUMN_NAME_DATE)));
295+
dt = dateFormat.parse(c.getString(c.getColumnIndex(Bill.BillEntry.COLUMN_NAME_DATE)));
286296
b.setDate(dt);
287297
} catch (ParseException e) {
288298
e.printStackTrace();
@@ -305,4 +315,5 @@ public long createItem(Item item) {
305315
return db.insert(Item.ItemEntry.TABLE_NAME, null, values);
306316
}
307317

318+
308319
}

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
android:layout_width="match_parent"
1313
android:layout_height="wrap_content"
1414
android:orientation="horizontal">
15+
1516
<TextView
1617
android:text="Place: "
1718
android:layout_width="80sp"
@@ -20,10 +21,11 @@
2021
android:textSize="16sp" />
2122
<TextView
2223
android:text="Place"
23-
android:layout_width="wrap_content"
24+
android:layout_width="0dp"
2425
android:layout_height="wrap_content"
2526
android:textSize="16sp"
26-
android:id="@+id/bill_detail_textView_place" />
27+
android:id="@+id/bill_detail_textView_place"
28+
android:layout_weight="1"/>
2729
</LinearLayout>
2830

2931
<LinearLayout
@@ -38,10 +40,11 @@
3840
android:id="@+id/textView2" />
3941
<TextView
4042
android:text="Date"
41-
android:layout_width="wrap_content"
43+
android:layout_width="0dp"
4244
android:layout_height="wrap_content"
4345
android:textSize="16sp"
44-
android:id="@+id/bill_detail_textView_date" />
46+
android:id="@+id/bill_detail_textView_date"
47+
android:layout_weight="1"/>
4548
</LinearLayout>
4649

4750
<LinearLayout
@@ -56,10 +59,11 @@
5659
android:id="@+id/textView4" />
5760
<TextView
5861
android:text="Payer"
59-
android:layout_width="wrap_content"
62+
android:layout_width="0dp"
6063
android:layout_height="wrap_content"
6164
android:textSize="16sp"
62-
android:id="@+id/bill_detail_textView_payer" />
65+
android:id="@+id/bill_detail_textView_payer"
66+
android:layout_weight="1"/>
6367
</LinearLayout>
6468

6569

@@ -72,9 +76,23 @@
7276

7377
</android.support.v7.widget.RecyclerView>
7478

75-
<Button
76-
android:text="Add Item"
79+
<LinearLayout
7780
android:layout_width="match_parent"
7881
android:layout_height="wrap_content"
79-
android:id="@+id/bill_detail_button" />
82+
android:orientation="horizontal">
83+
<Button
84+
android:text="Add Item"
85+
android:layout_width="0dp"
86+
android:layout_height="wrap_content"
87+
android:layout_weight="1"
88+
android:id="@+id/bill_detail_add_button" />
89+
90+
<Button
91+
android:text="Edit"
92+
android:layout_width="0dp"
93+
android:layout_height="wrap_content"
94+
android:layout_weight="1"
95+
android:id="@+id/bill_detail_edit_button" />
96+
</LinearLayout>
97+
8098
</LinearLayout>

0 commit comments

Comments
 (0)