Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 1a55d3a

Browse files
author
Evgenii Kanivets
committed
[30m]. Fix the bug of absent category.
1 parent 884e0af commit 1a55d3a

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/CategoryController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.ContentValues;
44
import android.database.Cursor;
55
import android.database.sqlite.SQLiteDatabase;
6+
import android.util.Log;
67

78
import com.blogspot.e_kanivets.moneytracker.helper.DbHelper;
89
import com.blogspot.e_kanivets.moneytracker.model.Category;
@@ -17,6 +18,8 @@
1718
* @author Evgenii Kanivets
1819
*/
1920
public class CategoryController {
21+
private static final String TAG = "CategoryController";
22+
2023
private DbHelper dbHelper;
2124

2225
public CategoryController(DbHelper dbHelper) {
@@ -59,6 +62,7 @@ public int addCategory(String name) {
5962
contentValues.put(DbHelper.NAME_COLUMN, name);
6063

6164
int id = (int) db.insert(DbHelper.TABLE_CATEGORIES, null, contentValues);
65+
Log.d(TAG, "created category with id = " + id);
6266

6367
db.close();
6468

app/src/main/java/com/blogspot/e_kanivets/moneytracker/controller/RecordController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ public List<Record> getRecords(Period period) {
110110

111111
public void addRecord(Record record) {
112112
//Add category if it does not exist yet
113-
if (categoryController.getCategoryIdByName(record.getCategory()) == -1)
113+
if (categoryController.getCategoryIdByName(record.getCategory()) == -1) {
114114
categoryController.addCategory(record.getCategory());
115+
record.setCategoryId(categoryController.getCategoryIdByName(record.getCategory()));
116+
}
115117

116118
//Add record to DB
117119
SQLiteDatabase db = dbHelper.getWritableDatabase();
@@ -133,8 +135,9 @@ public void addRecord(Record record) {
133135

134136
public void updateRecordById(int id, String title, String category, int price, int accountId) {
135137
//Add category if it does not exist yet
136-
if (categoryController.getCategoryIdByName(category) == -1)
138+
if (categoryController.getCategoryIdByName(category) == -1) {
137139
categoryController.addCategory(category);
140+
}
138141

139142
Record oldRecord = read(id);
140143

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/Record.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public Record(long time, int type, String title, String category, int price, int
3939
this.time = time;
4040
this.type = type;
4141
this.title = title;
42+
this.categoryId = -1;
4243
this.category = category;
4344
this.price = price;
4445
this.accountId = accountId;

0 commit comments

Comments
 (0)