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

Commit 398e11f

Browse files
author
Evgeniy Kanivets
committed
Added possibility to delete a row from DB
1 parent 7525a76 commit 398e11f

File tree

6 files changed

+53
-16
lines changed

6 files changed

+53
-16
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,36 @@ private void retrieveDataFromDB() {
115115

116116
Cursor cursor = db.query(Constants.TABLE_RECORDS, null, null, null, null, null, null);
117117

118-
List<Record> records = new ArrayList<Record>();
118+
final List<Record> records = new ArrayList<Record>();
119119

120120
if(cursor.moveToFirst()) {
121121
//Get indexes of columns
122+
int idColIndex = cursor.getColumnIndex("id");
122123
int titleColIndex = cursor.getColumnIndex("title");
123124
int categoryColIndex = cursor.getColumnIndex("category_id");
124125
int priceColIndex = cursor.getColumnIndex("price");
125126

126127
do {
127128
//Add record to list
128-
records.add(new Record(cursor.getString(titleColIndex),
129+
records.add(new Record(cursor.getInt(idColIndex),
130+
cursor.getString(titleColIndex),
129131
Integer.toString(cursor.getInt(categoryColIndex)),
130132
cursor.getString(priceColIndex)));
131133
} while (cursor.moveToNext());
132134
}
133135

134136
db.close();
135137

136-
listView.setAdapter(new RecordAdapter(activity, records));
138+
listView.setAdapter(new RecordAdapter(activity, records, new View.OnClickListener() {
139+
@Override
140+
public void onClick(View v) {
141+
SQLiteDatabase db = dbHelper.getWritableDatabase();
142+
db.delete(Constants.TABLE_RECORDS, "id=?",
143+
new String[] {Integer.toString(records.get((Integer)v.getTag()).getId())});
144+
db.close();
145+
retrieveDataFromDB();
146+
}
147+
}));
137148
((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
138149
}
139150
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.blogspot.e_kanivets.moneytracker.adapter;
22

33
import android.content.Context;
4+
import android.database.sqlite.SQLiteDatabase;
45
import android.view.LayoutInflater;
56
import android.view.View;
67
import android.view.ViewGroup;
78
import android.widget.BaseAdapter;
9+
import android.widget.Button;
810
import android.widget.TextView;
911

1012
import com.blogspot.e_kanivets.moneytracker.R;
13+
import com.blogspot.e_kanivets.moneytracker.helper.DBHelper;
1114
import com.blogspot.e_kanivets.moneytracker.model.Record;
15+
import com.blogspot.e_kanivets.moneytracker.util.Constants;
1216

1317
import java.util.List;
1418

@@ -20,9 +24,12 @@ public class RecordAdapter extends BaseAdapter{
2024
private Context context;
2125
private List<Record> records;
2226

23-
public RecordAdapter(Context context, List<Record> records) {
27+
private View.OnClickListener onClickListener;
28+
29+
public RecordAdapter(Context context, List<Record> records, View.OnClickListener onClickListener) {
2430
this.context = context;
2531
this.records = records;
32+
this.onClickListener = onClickListener;
2633
}
2734

2835
@Override
@@ -41,18 +48,23 @@ public long getItemId(int position) {
4148
}
4249

4350
@Override
44-
public View getView(int position, View convertView, ViewGroup parent) {
51+
public View getView(final int position, View convertView, ViewGroup parent) {
4552
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
4653
convertView = layoutInflater.inflate(R.layout.view_record, null);
4754

4855
TextView tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
4956
TextView tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
5057
TextView tvCategory = (TextView) convertView.findViewById(R.id.tv_category);
5158

59+
Button bRemove = (Button) convertView.findViewById(R.id.b_remove);
60+
5261
tvPrice.setText(records.get(position).getPrice());
5362
tvTitle.setText(records.get(position).getTitle());
5463
tvCategory.setText(records.get(position).getCategory());
5564

65+
bRemove.setTag(position);
66+
bRemove.setOnClickListener(onClickListener);
67+
5668
return convertView;
5769
}
5870
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public DBHelper(Context context) {
1717

1818
@Override
1919
public void onCreate(SQLiteDatabase db) {
20-
db.execSQL("create table " + Constants.TABLE_RECORDS + "("
21-
+ "id integer primary key autoincrement,"
22-
+ "time integer,"
23-
+ "type integer,"
24-
+ "title text,"
25-
+ "category_id integer,"
26-
+ "price integer" + ");");
20+
db.execSQL("CREATE TABLE " + Constants.TABLE_RECORDS + "("
21+
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
22+
+ "time INTEGER,"
23+
+ "type INTEGER,"
24+
+ "title TEXT,"
25+
+ "category_id INTEGER,"
26+
+ "price INTEGER" + ");");
2727
}
2828

2929
@Override

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55
*/
66
public class Record {
77

8+
private int id;
9+
810
private String title;
911
private String category;
1012
private String price;
1113

12-
public Record(String title, String category, String price) {
14+
public Record(int id, String title, String category, String price) {
15+
this.id = id;
1316
this.title = title;
1417
this.category = category;
1518
this.price = price;
1619
}
1720

21+
public int getId() {
22+
return id;
23+
}
24+
1825
public String getTitle() {
1926
return title;
2027
}
681 Bytes
Loading

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<TextView
1515
android:layout_width="match_parent"
16-
android:layout_height="wrap_content"
16+
android:layout_height="match_parent"
1717
android:text="New Text"
1818
android:id="@+id/tv_price"
1919
android:textSize="20dp"
@@ -22,7 +22,7 @@
2222

2323
<TextView
2424
android:layout_width="match_parent"
25-
android:layout_height="wrap_content"
25+
android:layout_height="match_parent"
2626
android:text="New Text"
2727
android:id="@+id/tv_title"
2828
android:textSize="20dp"
@@ -31,11 +31,18 @@
3131

3232
<TextView
3333
android:layout_width="match_parent"
34-
android:layout_height="wrap_content"
34+
android:layout_height="match_parent"
3535
android:text="New Text"
3636
android:id="@+id/tv_category"
3737
android:textSize="20dp"
3838
android:layout_weight="1"
3939
android:gravity="center" />
40+
41+
<Button
42+
android:layout_width="40dp"
43+
android:layout_height="40dp"
44+
android:id="@+id/b_remove"
45+
android:background="@drawable/ic_action_remove" />
46+
4047
</LinearLayout>
4148
</LinearLayout>

0 commit comments

Comments
 (0)