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

Commit 623968f

Browse files
author
evgenii
committed
Added a displaying and adding of accounts.
1 parent 4be2023 commit 623968f

File tree

9 files changed

+225
-14
lines changed

9 files changed

+225
-14
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.blogspot.e_kanivets.moneytracker.adapter;
2+
3+
import android.content.Context;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.BaseAdapter;
8+
import android.widget.TextView;
9+
10+
import com.blogspot.e_kanivets.moneytracker.R;
11+
import com.blogspot.e_kanivets.moneytracker.model.Account;
12+
13+
import java.util.List;
14+
15+
/**
16+
* Custom adapter class for Account entity
17+
* Created by evgenii on 6/3/15.
18+
*/
19+
public class AccountAdapter extends BaseAdapter {
20+
21+
private Context context;
22+
private List<Account> accounts;
23+
24+
public AccountAdapter(Context context, List<Account> accounts) {
25+
this.context = context;
26+
this.accounts = accounts;
27+
28+
}
29+
30+
@Override
31+
public int getCount() {
32+
return accounts.size();
33+
}
34+
35+
@Override
36+
public Object getItem(int position) {
37+
return accounts.get(position);
38+
}
39+
40+
@Override
41+
public long getItemId(int position) {
42+
return position;
43+
}
44+
45+
@Override
46+
public View getView(final int position, View convertView, ViewGroup parent) {
47+
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
48+
convertView = layoutInflater.inflate(R.layout.view_account, null);
49+
50+
TextView tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
51+
TextView tvCurSum = (TextView) convertView.findViewById(R.id.tv_cur_sum);
52+
53+
tvTitle.setText(accounts.get(position).getTitle());
54+
tvCurSum.setText(Integer.toString(accounts.get(position).getCurSum()));
55+
56+
return convertView;
57+
}
58+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/adapter/RecordAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@ public View getView(final int position, View convertView, ViewGroup parent) {
8080

8181
return convertView;
8282
}
83-
}
83+
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/fragment/AccountsFragment.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,27 @@
88
import android.view.LayoutInflater;
99
import android.view.View;
1010
import android.view.ViewGroup;
11+
import android.widget.BaseAdapter;
12+
import android.widget.ListView;
1113

1214
import com.blogspot.e_kanivets.moneytracker.R;
1315
import com.blogspot.e_kanivets.moneytracker.activity.NavDrawerActivity;
16+
import com.blogspot.e_kanivets.moneytracker.adapter.AccountAdapter;
17+
import com.blogspot.e_kanivets.moneytracker.adapter.RecordAdapter;
18+
import com.blogspot.e_kanivets.moneytracker.helper.MTHelper;
1419

1520
/**
1621
* A simple {@link Fragment} subclass.
1722
* Use the {@link AccountsFragment#newInstance} factory method to
1823
* create an instance of this fragment.
1924
*/
20-
public class AccountsFragment extends Fragment {
25+
public class AccountsFragment extends Fragment implements View.OnClickListener {
2126
private static final String KEY_POSITION = "key_position";
2227

2328
private int position;
2429

30+
private ListView listView;
31+
2532
/**
2633
* Use this factory method to create a new instance of
2734
* this fragment using the provided parameters.
@@ -66,8 +73,26 @@ public void onAttach(Activity activity) {
6673
((NavDrawerActivity) activity).onSectionAttached(position);
6774
}
6875

76+
@Override
77+
public void onClick(View v) {
78+
switch (v.getId()) {
79+
case R.id.btn_add_account:
80+
MTHelper.getInstance().addAccount("Cash", 2000);
81+
break;
82+
83+
default:
84+
break;
85+
}
86+
}
87+
6988
private void initViews(View rootView) {
7089
if (rootView != null) {
90+
listView = (ListView) rootView.findViewById(R.id.list_view);
91+
92+
rootView.findViewById(R.id.btn_add_account).setOnClickListener(this);
93+
94+
listView.setAdapter(new AccountAdapter(getActivity(), MTHelper.getInstance().getAccounts()));
95+
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
7196
}
7297
}
7398

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.database.Cursor;
55
import android.database.sqlite.SQLiteDatabase;
66

7+
import com.blogspot.e_kanivets.moneytracker.model.Account;
78
import com.blogspot.e_kanivets.moneytracker.model.Category;
89
import com.blogspot.e_kanivets.moneytracker.model.Period;
910
import com.blogspot.e_kanivets.moneytracker.model.Record;
@@ -29,6 +30,7 @@ public class MTHelper extends Observable {
2930

3031
private List<Category> categories;
3132
private List<Record> records;
33+
private List<Account> accounts;
3234

3335
private Period period;
3436

@@ -45,6 +47,7 @@ private MTHelper() {
4547
initPeriod();
4648
categories = new ArrayList<>();
4749
records = new ArrayList<>();
50+
accounts = new ArrayList<>();
4851
}
4952

5053
public void initialize() {
@@ -109,6 +112,27 @@ public void initialize() {
109112
} while (cursor.moveToNext());
110113
}
111114

115+
// Read accounts table from db
116+
cursor = db.query(DBHelper.TABLE_ACCOUNTS, null, null, null, null, null, null);
117+
accounts.clear();
118+
119+
if (cursor.moveToFirst()) {
120+
// Get indexes of columns
121+
int idColIndex = cursor.getColumnIndex(DBHelper.ID_COLUMN);
122+
int titleColIndex = cursor.getColumnIndex(DBHelper.TITLE_COLUMN);
123+
int curSumColIndex = cursor.getColumnIndex(DBHelper.CUR_SUM_COLUMN);
124+
125+
do {
126+
// Read a account from DB
127+
Account account = new Account(cursor.getInt(idColIndex),
128+
cursor.getString(titleColIndex),
129+
cursor.getInt(curSumColIndex));
130+
131+
//Add account to list
132+
accounts.add(account);
133+
} while (cursor.moveToNext());
134+
}
135+
112136
db.close();
113137
}
114138

@@ -183,6 +207,10 @@ public List<Record> getRecords() {
183207
return records;
184208
}
185209

210+
public List<Account> getAccounts() {
211+
return accounts;
212+
}
213+
186214
public void addRecord(long time, int type, String title, String category, int price) {
187215
//Add category if it does not exist yet
188216
if (getCategoryIdByName(category) == -1) {
@@ -358,4 +386,21 @@ public String getLastDay() {
358386
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
359387
return dateFormat.format(period.getLast());
360388
}
389+
390+
public void addAccount(String title, int curSum) {
391+
//Add account to DB
392+
SQLiteDatabase db = dbHelper.getWritableDatabase();
393+
394+
ContentValues contentValues = new ContentValues();
395+
contentValues.put(DBHelper.TITLE_COLUMN, title);
396+
contentValues.put(DBHelper.CUR_SUM_COLUMN, curSum);
397+
398+
int id = (int) db.insert(DBHelper.TABLE_ACCOUNTS, null, contentValues);
399+
400+
db.close();
401+
402+
//notify observers
403+
setChanged();
404+
notifyObservers();
405+
}
361406
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.blogspot.e_kanivets.moneytracker.model;
2+
3+
/**
4+
* Entity class for account
5+
* Created by evgenii on 6/3/15.
6+
*/
7+
public class Account {
8+
private int id;
9+
private String title;
10+
private int curSum;
11+
12+
public Account(int id, String title, int curSum) {
13+
this.title = title;
14+
this.curSum = curSum;
15+
}
16+
17+
public String getTitle() {
18+
return title;
19+
}
20+
21+
public void setTitle(String title) {
22+
this.title = title;
23+
}
24+
25+
public int getCurSum() {
26+
return curSum;
27+
}
28+
29+
public void setCurSum(int curSum) {
30+
this.curSum = curSum;
31+
}
32+
}
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
3-
android:layout_height="match_parent"
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="fill_parent"
4+
android:layout_height="fill_parent"
5+
android:background="@color/light_grey"
6+
android:orientation="vertical"
47
tools:context="com.blogspot.e_kanivets.moneytracker.fragment.AccountsFragment">
58

6-
<!-- TODO: Update blank fragment layout -->
7-
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
8-
android:text="@string/hello_blank_fragment" />
9+
<ListView
10+
android:id="@+id/list_view"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
android:layout_gravity="center_horizontal"
14+
android:layout_weight="1"
15+
android:background="@android:color/transparent" />
916

10-
</FrameLayout>
17+
<LinearLayout
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:layout_gravity="center_horizontal"
21+
android:orientation="horizontal">
22+
23+
<Button
24+
android:id="@+id/btn_add_account"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:layout_weight="1"
28+
android:background="@drawable/selector_add_income"
29+
android:padding="15dp"
30+
android:text="@string/add_account" />
31+
</LinearLayout>
32+
33+
</LinearLayout>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<LinearLayout
2-
xmlns:android="http://schemas.android.com/apk/res/android"
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
32
android:layout_width="fill_parent"
43
android:layout_height="fill_parent"
54
android:background="@color/light_grey"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="horizontal">
6+
7+
<TextView
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:text="New Text"
11+
android:id="@+id/tv_title"
12+
android:textSize="18sp"
13+
android:layout_weight="1"
14+
android:layout_gravity="center_vertical|top"
15+
android:gravity="left"
16+
android:layout_marginLeft="10dp"
17+
android:background="@android:color/transparent"
18+
android:paddingBottom="5dp" />
19+
20+
<TextView
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content"
23+
android:text="New Text"
24+
android:id="@+id/tv_cur_sum"
25+
android:textSize="18sp"
26+
android:layout_weight="1.15"
27+
android:background="@android:color/transparent"
28+
android:gravity="right"
29+
android:layout_marginRight="10dp" />
30+
31+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
<string name="navigation_drawer_close">Close navigation drawer</string>
5353

5454
<string name="export">Export</string>
55-
56-
<!-- TODO: Remove or change this placeholder text -->
57-
<string name="hello_blank_fragment">Hello blank fragment</string>
55+
<string name="add_account">Add account</string>
5856

5957
</resources>

0 commit comments

Comments
 (0)