· 7 years ago · Feb 12, 2019, 11:38 PM
1public class DatabaseHelper_bp extends SQLiteOpenHelper {
2
3 private static final String DATABASE_NAME = "bpDB";
4 private static final int DATABASE_VERSION = 1;
5
6 // Database creation sql statement
7 private static final String DATABASE_CREATE = "create table bp_import ( _id integer primary key, datetime text not null, systolic text not null, diastolic text not null, pulses text not null, notes text not null);";
8
9 public DatabaseHelper_bp(Context context) {
10 super(context, DATABASE_NAME, null, DATABASE_VERSION);
11 }
12
13 // Method is called during creation of the database
14 @Override
15 public void onCreate(SQLiteDatabase database) {
16 database.execSQL(DATABASE_CREATE);
17 }
18
19 // Method is called during an upgrade of the database,
20 @Override
21 public void onUpgrade(SQLiteDatabase database, int oldVersion,
22 int newVersion) {
23 Log.w(DatabaseHelper_bp.class.getName(),
24 "Upgrading database from version " + oldVersion + " to "
25 + newVersion + ", which will destroy all old data");
26 database.execSQL("DROP TABLE IF EXISTS bp_import");
27 onCreate(database);
28 }
29}
30
31public class BpDAO {
32
33 private DatabaseHelper_bp dbHelper;
34
35 private SQLiteDatabase database;
36 /**
37 * Movie table related constants.
38 */
39 public final static String bp_TABLE = "bp_import";
40 public final static String bp_ID = "_id";
41 public final static String bp_DT = "datetime";
42 public final static String bp_SYS = "systolic";
43 public final static String bp_DIA = "diastolic";
44 public final static String bp_PUL = "pulses";
45 public final static String bp_NOT = "notes";
46
47 /**
48 *
49 * @param context
50 */
51 public BpDAO(Context context) {
52 dbHelper = new DatabaseHelper_bp(context);
53 database = dbHelper.getWritableDatabase();
54 }
55
56 /**
57 * Creates a new blood pressure measure
58 *
59 * @param datetime
60 * @param systolic
61 * @param diastolic
62 * @param pulses
63 * @param notes
64 * @return
65 */
66 public long importBP(String datetime, String systolic, String diastolic,
67 String pulses, String notes) {
68 ContentValues values = new ContentValues();
69 values.put(bp_DT, datetime);
70 values.put(bp_SYS, systolic);
71 values.put(bp_DIA, diastolic);
72 values.put(bp_PUL, pulses);
73 values.put(bp_NOT, notes);
74 return database.insert(bp_TABLE, null, values);
75 }
76
77 /**
78 * Fetch all movies
79 *
80 * @return
81 */
82 public Cursor fetchAll_bp() {
83 Cursor mCursor = database.query(true, bp_TABLE, new String[] { bp_SYS,
84 bp_DIA, bp_DT, bp_ID }, null, null, null, null, null, null);
85 if (mCursor != null) {
86 mCursor.moveToFirst();
87 }
88 return mCursor;
89 }
90}
91
92public class HistoryActivity extends ListActivity {
93
94private BpDAO dao;
95
96private SimpleCursorAdapter dbAdapter;
97
98@Override
99protected void onCreate(Bundle savedInstanceState) {
100 // TODO Auto-generated method stub
101 super.onCreate(savedInstanceState);
102 dao = new BpDAO(this);
103 Cursor bpList = dao.fetchAll_bp();
104 String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
105 int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
106 R.id.bpDtHolder };
107 dbAdapter = new SimpleCursorAdapter(this, R.layout.history_bp, bpList,
108 from, target);
109 setListAdapter(dbAdapter);
110}
111 //above is all good!
112// try to make new activity on click - let's
113// see...down here is the problem!
114@Override
115public void onListItemClick(ListView l, View view, int position, long id) {
116 // TODO Auto-generated method stub
117 super.onListItemClick(l, view, position, id);
118 Log.d("BPT", "Selected bp id =" + id);
119 // log says that i have selected an item with id : 11
120
121 Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
122
123 String bp_DT = selectedBpDetails.getString(selectedBpDetails
124 .getColumnIndex(BpDAO.bp_DT));
125 String bp_SYS = selectedBpDetails.getString(selectedBpDetails
126 .getColumnIndex(BpDAO.bp_SYS));
127 String bp_DIA = selectedBpDetails.getString(selectedBpDetails
128 .getColumnIndex(BpDAO.bp_DIA));
129 String bp_PUL = selectedBpDetails.getString(selectedBpDetails
130 .getColumnIndex(BpDAO.bp_PUL));
131 String bp_NOT = selectedBpDetails.getString(selectedBpDetails
132 .getColumnIndex(BpDAO.bp_NOT));
133
134 Log.d("BPT", "Selected bp details = { date=" + bp_DT + ", systolic="
135 + bp_SYS + ", diastolic=" + bp_DIA + ", pulses=" + bp_PUL
136 + ", notes=" + bp_NOT + " }");
137
138 Intent intent = new Intent(HistoryActivity.this, FromHistory.class);
139 intent.putExtra("bp_SYS", bp_SYS);
140 intent.putExtra("bp_DIA", bp_DIA);
141 intent.putExtra("bp_DT", bp_DT);
142 intent.putExtra("bp_PUL", bp_PUL);
143 intent.putExtra("bp_NOT", bp_NOT);
144 startActivity(intent);
145}
146// ----------------------------------------------------------------------------
147
14816:50:48.513: D/BPT(562): Selected bp id =11
14916:50:48.513: **E/CursorWindow(562): Failed to read row 1, column -1 from a CursorWindow which has 13 rows, 4 columns.**
150
151Cursor selectedBpDetails = (Cursor) l.getItemAtPosition(position);
152if (selectedBpDetails.getCursor() > 0) {
153 selectedBpDetails.moveToFirst();
154
155 String bp_DT = selectedBpDetails.getString(selectedBpDetails
156 .getColumnIndex(BpDAO.bp_DT));
157 String bp_SYS = selectedBpDetails.getString(selectedBpDetails
158 .getColumnIndex(BpDAO.bp_SYS));
159 String bp_DIA = selectedBpDetails.getString(selectedBpDetails
160 .getColumnIndex(BpDAO.bp_DIA));
161 String bp_PUL = selectedBpDetails.getString(selectedBpDetails
162 .getColumnIndex(BpDAO.bp_PUL));
163 String bp_NOT = selectedBpDetails.getString(selectedBpDetails
164 .getColumnIndex(BpDAO.bp_NOT));
165}