· 8 years ago · Aug 15, 2018, 02:38 PM
1Database being overwritten ( have to open the app twice to display correctly )?
2c = initSpinner(c);
3
4private void fillSpinner(){
5
6 DbAdapter mDbHelper = new DbAdapter(context);
7 mDbHelper.open("names");
8
9 Cursor c = mDbHelper.fetchAllRows();
10 ArrayList<String> a = new ArrayList<String>();
11
12 if ( c.getPosition() != 0 && c.moveToFirst() == (false) ){
13// c = initSpinner(c); //commented out to prevent creating a new //database.
14 }
15
16public class DbAdapter {
17
18// Database fields
19public static final String KEY_ROWID = "_id";
20public static final String KEY_NAME = "name";
21
22static final String DATABASE_TABLE = "names";
23private Context context;
24private SQLiteDatabase database;
25private DbHelper dbHelper;
26
27
28public DbAdapter(Context ctx) {
29 context = ctx;
30
31}
32
33public SQLiteDatabase openToRead() throws SQLException {
34 dbHelper = new DbHelper(context);
35 database = dbHelper.getReadableDatabase();
36
37 return database;
38}
39
40public SQLiteDatabase open(String dbname) throws SQLException {
41 dbHelper = new DbHelper(context);
42 database = dbHelper.getWritableDatabase();
43 return database;
44}
45
46public void close() {
47 dbHelper.close();
48}
49
50//
51/**
52 * Create a new todo If the todo is successfully created return the new
53 * rowId for that note, otherwise return a -1 to indicate failure.
54 */
55
56public long createRow(String name) {
57 ContentValues initialValues = createContentValues(name);
58 return database.insert(DATABASE_TABLE, null, initialValues);
59}
60
61public long createRowWithAL(ArrayList<String> values){
62
63 ContentValues v = ArrayListToContentValues(values);
64
65 return database.insert(DATABASE_TABLE, null, v);
66
67}
68
69private ContentValues ArrayListToContentValues(ArrayList<String> parcel){
70
71 ContentValues values = new ContentValues();
72
73// values.put(KEY_ROWID, _id);
74 values.put(KEY_NAME, parcel.get(0));
75 return values;
76}
77
78
79
80/**
81 * Update the todo
82 */
83
84public boolean updateRows(long rowId,
85 String name) {
86
87 ContentValues updateValues = createContentValues(
88 name);
89
90 return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
91 + rowId, null) > 0;
92}
93
94
95/**
96 * Deletes todo
97 */
98
99public boolean deleteRow(long rowId) {
100 return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
101}
102
103
104/**
105 * Return a Cursor over the list of all todo in the database
106 *
107 * @return Cursor over all notes
108 */
109
110public Cursor fetchAllRows() {
111 return database.query(DATABASE_TABLE, null, null, null, null,
112 null, null);
113}
114
115
116/**
117 * Return a Cursor positioned at the defined todo
118 */
119
120public Cursor fetchRow(long rowId) throws SQLException {
121 Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
122 KEY_ROWID ,KEY_NAME},
123 KEY_ROWID + "=" + rowId, null, null, null, null, null);
124 if (mCursor != null) {
125 mCursor.moveToFirst();
126 }
127 return mCursor;
128}
129
130
131private ContentValues createContentValues(String name){
132
133 ContentValues values = new ContentValues();
134
135 values.put(KEY_NAME, name);
136 return values;
137}
138
139//returns (an) entire column(s), all rows
140public Cursor fetchColumns(String[] colnames) {
141 Cursor mCursor = database.query(DATABASE_TABLE, colnames, null,
142 null, null, null, null);
143 if (mCursor != null) {
144 mCursor.moveToFirst();
145 }
146 return mCursor;
147}
148
149public class DbHelper extends SQLiteOpenHelper {
150
151Context context;
152
153private static final String DATABASE_NAME = "names";
154private static final int DATABASE_VERSION = 1;
155
156// Database creation sql statement
157private static final String DATABASE_CREATE = "create table if not exists "+DATABASE_NAME+" (_id integer primary key autoincrement, " +
158 "name text not null);";
159
160public DbHelper(Context context) {
161 super(context, DATABASE_NAME, null, DATABASE_VERSION);
162
163}
164
165// Method is called during creation of the database
166@Override
167public void onCreate(SQLiteDatabase database) {
168
169 database.execSQL(DATABASE_CREATE);
170}
171
172// Method is called during an upgrade of the database, e.g. if you increase
173// the database version
174@Override
175public void onUpgrade(SQLiteDatabase database, int oldVersion,
176 int newVersion) {
177 Log.w(DbHelper.class.getName(),
178 "Upgrading database from version " + oldVersion + " to "
179 + newVersion + ", which will destroy all old data");
180 database.execSQL("DROP TABLE IF EXISTS names");
181 onCreate(database);
182
183private void fillSpinner(){
184
185 DbAdapter mDbHelper = new DbAdapter(context);
186 mDbHelper.open("name");
187 mDbHelper.createRow("Add Name"); //added this
188 mDbHelper.close(); //added this
189 downloadFile(); //added this
190 mDbHelper.open("names");
191
192 Cursor c = mDbHelper.fetchAllRows();
193 ArrayList<String> a = new ArrayList<String>();
194
195 if ( c.getPosition() != 0 && c.moveToFirst() == (false) ){
196 c.close();
197
198 mDbHelper.createRow("Add Name");
199 c = mDbHelper.fetchColumns(new String[] {"_id","name"});
200 }
201
202private void fillSpinner(){
203
204DbAdapter mDbHelper = new DbAdapter(context);
205mDbHelper.open("name");
206 mDbHelper.createRow("Add Name"); //added this
207mDbHelper.close(); //added this
208downloadFile(); //added this
209mDbHelper.open("names");
210
211Cursor c = mDbHelper.fetchAllRows();
212ArrayList<String> a = new ArrayList<String>();
213
214if ( c.getPosition() != 0 && c.moveToFirst() == (false) ){
215 c.close();
216
217 mDbHelper.createRow("Add Name");
218 c = mDbHelper.fetchColumns(new String[] {"_id","name"});
219}