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

Commit fe809f9

Browse files
author
Evgeniy Kanivets
committed
Added database insert opertaions
1 parent fa6af11 commit fe809f9

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

app/src/main/java/com/blogspot/e_kanivets/moneytracker/activity/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.view.MenuItem;
88
import android.view.View;
99
import android.widget.Button;
10+
import android.widget.ListView;
1011

1112
import com.blogspot.e_kanivets.moneytracker.R;
1213
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
@@ -21,6 +22,8 @@ public class MainActivity extends ActionBarActivity {
2122

2223
private DBHelper dbHelper;
2324

25+
private ListView listView;
26+
2427
private Button btnAddIncome;
2528
private Button btnAddExpense;
2629

@@ -35,6 +38,8 @@ protected void onCreate(Bundle savedInstanceState) {
3538
btnAddIncome = (Button) findViewById(R.id.b_add_income);
3639
btnAddExpense = (Button) findViewById(R.id.b_add_expense);
3740

41+
listView = (ListView) findViewById(R.id.listView);
42+
3843
//Set listeners
3944
btnAddIncome.setOnClickListener(new View.OnClickListener() {
4045
@Override

app/src/main/java/com/blogspot/e_kanivets/moneytracker/helper/DBHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void onCreate(SQLiteDatabase db) {
2020
db.execSQL("create table " + Constants.TABLE_RECORDS + "("
2121
+ "id integer primary key autoincrement,"
2222
+ "time integer,"
23+
+ "type integer,"
2324
+ "title text,"
2425
+ "category_id integer,"
2526
+ "price integer" + ");");

app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/AddExpenseDialog.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.blogspot.e_kanivets.moneytracker.ui;
22

33
import android.app.AlertDialog;
4+
import android.content.ContentValues;
45
import android.content.Context;
6+
import android.database.sqlite.SQLiteDatabase;
57
import android.os.Bundle;
8+
import android.util.Log;
69
import android.view.View;
710
import android.widget.Button;
11+
import android.widget.EditText;
812
import android.widget.TextView;
913

1014
import com.blogspot.e_kanivets.moneytracker.R;
1115
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
16+
import com.blogspot.e_kanivets.moneytracker.util.Constants;
1217
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
1318

1419
/**
@@ -30,7 +35,7 @@ public AddExpenseDialog(Context context) {
3035
protected void onCreate(Bundle savedInstanceState) {
3136
super.onCreate(savedInstanceState);
3237

33-
View layout = getLayoutInflater().inflate(R.layout.dialog_add_record, null);
38+
final View layout = getLayoutInflater().inflate(R.layout.dialog_add_record, null);
3439

3540
AlertDialog.Builder builder = new AlertDialog.Builder(context);
3641
builder.setView(layout);
@@ -46,6 +51,26 @@ protected void onCreate(Bundle savedInstanceState) {
4651
buttonAdd.setOnClickListener(new View.OnClickListener() {
4752
@Override
4853
public void onClick(View v) {
54+
//Init variables for inserting record to DB
55+
ContentValues contentValues = new ContentValues();
56+
57+
String title = ((EditText) layout.findViewById(R.id.et_title)).getText().toString();
58+
String category = ((EditText) layout.findViewById(R.id.et_category)).getText().toString();
59+
String price = ((EditText) layout.findViewById(R.id.et_price)).getText().toString();
60+
61+
SQLiteDatabase db = dbHelper.getWritableDatabase();
62+
63+
contentValues.put("type", "1");
64+
contentValues.put("title", title);
65+
contentValues.put("category_id", 1);
66+
contentValues.put("price", price);
67+
68+
long rowId = db.insert(Constants.TABLE_RECORDS, null, contentValues);
69+
70+
Log.d(Constants.TAG, "rowId = " + rowId);
71+
72+
db.close();
73+
4974
dialog.dismiss();
5075
}
5176
});

app/src/main/java/com/blogspot/e_kanivets/moneytracker/ui/AddIncomeDialog.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.blogspot.e_kanivets.moneytracker.ui;
22

33
import android.app.AlertDialog;
4+
import android.content.ContentValues;
45
import android.content.Context;
6+
import android.database.sqlite.SQLiteDatabase;
57
import android.os.Bundle;
8+
import android.util.Log;
69
import android.view.View;
710
import android.widget.Button;
11+
import android.widget.EditText;
812
import android.widget.TextView;
913

1014
import com.blogspot.e_kanivets.moneytracker.R;
1115
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
16+
import com.blogspot.e_kanivets.moneytracker.util.Constants;
1217
import com.blogspot.e_kanivets.moneytracker.util.MTApp;
1318

1419
/**
@@ -30,7 +35,7 @@ public AddIncomeDialog(Context context) {
3035
protected void onCreate(Bundle savedInstanceState) {
3136
super.onCreate(savedInstanceState);
3237

33-
View layout = getLayoutInflater().inflate(R.layout.dialog_add_record, null);
38+
final View layout = getLayoutInflater().inflate(R.layout.dialog_add_record, null);
3439

3540
AlertDialog.Builder builder = new AlertDialog.Builder(context);
3641
builder.setView(layout);
@@ -46,6 +51,26 @@ protected void onCreate(Bundle savedInstanceState) {
4651
buttonAdd.setOnClickListener(new View.OnClickListener() {
4752
@Override
4853
public void onClick(View v) {
54+
//Init variables for inserting record to DB
55+
ContentValues contentValues = new ContentValues();
56+
57+
String title = ((EditText) layout.findViewById(R.id.et_title)).getText().toString();
58+
String category = ((EditText) layout.findViewById(R.id.et_category)).getText().toString();
59+
String price = ((EditText) layout.findViewById(R.id.et_price)).getText().toString();
60+
61+
SQLiteDatabase db = dbHelper.getWritableDatabase();
62+
63+
contentValues.put("type", "0");
64+
contentValues.put("title", title);
65+
contentValues.put("category_id", 1);
66+
contentValues.put("price", price);
67+
68+
long rowId = db.insert(Constants.TABLE_RECORDS, null, contentValues);
69+
70+
Log.d(Constants.TAG, "rowId = " + rowId);
71+
72+
db.close();
73+
4974
dialog.dismiss();
5075
}
5176
});

app/src/main/res/layout/dialog_add_record.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<EditText
6969
android:layout_width="match_parent"
7070
android:layout_height="match_parent"
71-
android:id="@+id/et_expense_category"
71+
android:id="@+id/et_category"
7272
android:layout_weight="1"
7373
android:layout_marginRight="10dp" />
7474

@@ -93,9 +93,10 @@
9393
<EditText
9494
android:layout_width="match_parent"
9595
android:layout_height="wrap_content"
96-
android:id="@+id/et_expense_amount"
96+
android:id="@+id/et_price"
9797
android:layout_weight="1"
98-
android:layout_marginRight="10dp" />
98+
android:layout_marginRight="10dp"
99+
android:inputType="numberSigned" />
99100

100101
</LinearLayout>
101102
</LinearLayout>

0 commit comments

Comments
 (0)