· 8 years ago · Sep 02, 2018, 07:22 AM
1How do i display data from my database in a listview?
2package com.android.ideos;
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.util.Log;
10
11/**
12 * @author G Sam
13
14 *
15 */
16
17
18
19public class DBAdapter {
20 //defining columns(fields) in all 3 tables
21 public static final String KEY_CLIENTID = "_id";
22 public static final String KEY_TRANSID = "transId";
23 public static final String KEY_NAME = "name";
24 public static final String KEY_SURNAME = "surname";
25 public static final String KEY_MOBILE= "mobile";
26 public static final String KEY_TYPE = "Type";
27 public static final String KEY_DATETIME = "DateTime";
28 public static final String KEY_AMOUNT = "Amount";
29 public static final String KEY_BALANCE = "Balance";
30
31 private static final String TAG = "DBAdapter";
32
33 private static final String DATABASE_NAME = "radicalfinance";
34 private static final String DATABASE_CLIENTSTable = "clientstable";
35 private static final String DATABASE_TRANSACTIONS = "TransactionsTable";
36 private static final String DATABASE_CLIENTSBALANCE = "ClientsBalanceTable";
37 private static final int DATABASE_VERSION = 1;
38 //Creating the database radicalfinance
39 //CREATING CLIENTSTable
40 private static final String DATABASE_CREATE_CLIENTSTABLE =
41 "create table clientstable (_id integer primary key autoincrement, "
42 + "name text not null, surname text not null, "
43 + "mobile integer not null);";
44 //CREATING TransactionsTable
45 private static final String DATABASE_CREATE_TRANSACTIONSTABLE =
46 "create table TransactionsTable (_id integer primary key autoincrement, "
47 + "transId integer,"
48 + "Type boolean not null, DateTime text not null, "
49 + "Amount long not null);";
50 //CREATING ClientsBalanceTable
51 private static final String DATABASE_CREATE_CLIENTSBALANCETABLE =
52 "create table ClientsBalanceTable (_id integer primary key autoincrement, "
53 + "Balance long not null); ";
54
55
56 private final Context context;
57 private DatabaseHelper DBHelper;
58 private SQLiteDatabase db;
59
60
61 public DBAdapter(Context ctx)
62 {
63 this.context = ctx;
64 DBHelper = new DatabaseHelper(context);
65 }
66
67 public class DatabaseHelper extends SQLiteOpenHelper
68 {
69 DatabaseHelper(Context context)
70 {
71 super(context, DATABASE_NAME, null, DATABASE_VERSION);
72 }
73
74 @Override
75 public void onCreate(SQLiteDatabase db)
76 {
77 db.execSQL(DATABASE_CREATE_CLIENTSTABLE);
78 db.execSQL(DATABASE_CREATE_TRANSACTIONSTABLE);
79 db.execSQL(DATABASE_CREATE_CLIENTSBALANCETABLE);
80 }
81
82 @Override
83 public void onUpgrade(SQLiteDatabase db, int oldVersion,
84 int newVersion)
85 {
86 Log.w(TAG, "Upgrading database from version " + oldVersion
87 + " to "
88 + newVersion + ", which will destroy all old data");
89 db.execSQL("DROP TABLE IF EXISTS clientstable");
90 onCreate(db);
91 }
92 }
93
94 //methods for opening and closing the database, as well as the methods for adding/editing/deleting rows in the table.
95
96
97
98 //---opens the database---
99 public DBAdapter open() throws SQLException
100 {
101 db = DBHelper.getWritableDatabase();
102 return this;
103
104 }
105
106 //---closes the database---
107 public void close()
108 {
109 DBHelper.close();
110 }
111
112 //---insert a client and his info into the database---
113
114 public long insertClient(String name, String surname, String mobile)
115 {
116 ContentValues initialValues = new ContentValues();
117 initialValues.put(KEY_NAME, name);
118 initialValues.put(KEY_SURNAME, surname);
119 initialValues.put(KEY_MOBILE, mobile);
120 return db.insert(DATABASE_CLIENTSTable, null, initialValues);
121 }
122 public long insertClientTransaction(String transId, String Type, String DateTime, String Amount)
123 {
124 ContentValues initialValues = new ContentValues();
125 initialValues.put(KEY_TRANSID, transId);
126 initialValues.put(KEY_TYPE, Type);
127 initialValues.put(KEY_DATETIME, DateTime);
128 initialValues.put(KEY_AMOUNT, Amount);
129 return db.insert(DATABASE_TRANSACTIONS, null, initialValues);
130 }
131 public long insertClientBalance(String Balance)
132 {
133 ContentValues initialValues = new ContentValues();
134 initialValues.put(KEY_BALANCE, Balance);
135 return db.insert(DATABASE_CLIENTSBALANCE, null, initialValues);
136 }
137
138 //---deletes a particular client---
139 public boolean deleteClient(long clientId)
140 {
141 return db.delete(DATABASE_CLIENTSTable,KEY_CLIENTID + "=" + clientId, null) > 0;
142 }
143
144 //---retrieves all the clients---
145 public Cursor getAllClients()
146 {
147 return db.query(DATABASE_CLIENTSTable, new String[] {
148 KEY_CLIENTID,
149 KEY_NAME,
150 KEY_SURNAME,
151 KEY_MOBILE},
152 null,
153 null,
154 null,
155 null,
156 null);
157
158 }
159 //querying TransactionsTable
160 public Cursor getAllTransactionsRecords() {
161
162 return db.query(DATABASE_TRANSACTIONS, new String[] {
163 KEY_TRANSID,
164 KEY_TYPE,
165 KEY_DATETIME,
166 KEY_AMOUNT},
167 null,
168 null,
169 null,
170 null,
171 null);
172 }
173 //made comments will be taken care of i.e the clientsbalanceRecords
174 //querying clientsbalancetable
175 //public Cursor getAllBalanceRecords() {
176
177 //return db.query(DATABASE_CLIENTSBALANCE, new String[] {
178 //KEY_BALANCE},
179 //null,
180 //null);
181 //}
182
183 //---retrieves a particular client---
184 public Cursor getClient(long clientId) throws SQLException
185 {
186 Cursor mCursor =
187 db.query(true, DATABASE_CLIENTSTable, new String[] {
188 KEY_CLIENTID,
189 KEY_NAME,
190 KEY_SURNAME,
191 KEY_MOBILE
192 },
193 KEY_CLIENTID + "=" + clientId,
194 null,
195 null,
196 null,
197 null,
198 null);
199 if (mCursor != null) {
200 mCursor.moveToFirst();
201 }
202 return mCursor;
203 }
204
205 //---updates a client's details---
206 public boolean updateClient(long clientId, String name,
207 String surname, String mobile)
208 {
209 ContentValues args = new ContentValues();
210 args.put(KEY_NAME, name);
211 args.put(KEY_SURNAME, surname);
212 args.put(KEY_MOBILE, mobile);
213 return db.update(DATABASE_CLIENTSTable, args,
214 KEY_CLIENTID + "=" + clientId, null) > 0;
215 }
216 public boolean updateTransactions(long clientId, long transId, String Type,
217 String DateTime, long Amount)
218 {
219 ContentValues args = new ContentValues();
220 args.put(KEY_TRANSID, transId);
221 args.put(KEY_TYPE, Type);
222 args.put(KEY_DATETIME, DateTime);
223 args.put (KEY_AMOUNT, Amount);
224 return db.update(DATABASE_TRANSACTIONS, args,
225 KEY_CLIENTID + "=" + clientId, null) > 0;
226 }
227
228 public SQLiteDatabase getWritableDatabase() {
229 // TODO Auto-generated method stub
230 return null;
231 }
232}
233
234package com.android.ideos;
235import android.app.ListActivity;
236import android.database.Cursor;
237import android.os.Bundle;
238import android.view.Menu;
239import android.view.MenuInflater;
240import android.view.MenuItem;
241import android.widget.CursorAdapter;
242import android.widget.ListAdapter;
243import android.widget.Toast;
244public class ClientsActivity extends ListActivity {
245
246
247 protected DBAdapter db;
248 protected CursorAdapter dataSource;
249 protected ListAdapter adapter;
250 //private static final String columns[] = { "name", "surname"};
251 //private static final String TAGG = "ClientsActivity";
252
253
254
255 /** Called when the activity is first created. */
256 @Override
257 public void onCreate(Bundle savedInstanceState)
258 {
259 super.onCreate(savedInstanceState);
260 db = new DBAdapter(this);
261 /*DataBaseHelper helper = new DataBaseHelper(this);
262 db = helper.getWritableDatabase();
263 Cursor data = db.query("clientstable", columns,
264 null, null, null, null, null);
265 dataSource = new SimpleCursorAdapter(this,
266 R.layout.clients, data, columns,
267 new int[] { R.id.name, R.id.surname });
268
269 setListAdapter(dataSource);*/
270 db.open();
271 Long rowID = db.insertClient("Adera", "Dan", "0727858191");
272 db.close();
273
274 displayclients(rowID);
275
276 }
277
278 private void displayclients(long clientId)
279 **{
280 // TODO Auto-generated method stub
281 db.open();
282 Cursor results = db.getClient(clientId);
283 if (results.moveToFirst())
284 {
285 Toast.makeText(this, "Name: "+results.getString(1)+" "+results.getString(2), Toast.LENGTH_LONG).show();
286 }**
287
288
289
290
291
292}
293
294//calls the content menu layout
295 @Override
296 public boolean onCreateOptionsMenu(Menu menu) {
297 MenuInflater myMenuInflater = getMenuInflater();
298 myMenuInflater.inflate(R.menu.menu, menu);
299 return true;
300 }
301
302 // the layout of the menu as seen in the menu.xml file
303 @Override
304 public boolean onOptionsItemSelected(MenuItem item) {
305 // TODO Auto-generated method stub
306 switch(item.getItemId())
307 {
308 // the menu button New Client and the functionality code will be implemented here.
309 case(R.id.menu_new_client):
310 Toast.makeText(this, "New client", Toast.LENGTH_LONG).show();
311
312
313 break;
314
315 // the menu button: Edit, and the functionality code will be implemented here.
316 case(R.id.menu_edit):
317 Toast.makeText(this, "Edit", Toast.LENGTH_LONG).show();
318 break;
319
320 // the menu button: DElete, and the functionality code will be implemented here.
321 case(R.id.menu_delete):
322 Toast.makeText(this, "Delete", Toast.LENGTH_LONG).show();
323
324
325 break;
326
327 // the menu button: Search, and the functionality code will be implemented here.
328 case(R.id.menu_search):
329 Toast.makeText(this, "Search", Toast.LENGTH_LONG).show();
330 break;
331 }
332 return true;
333 }
334}
335
336private void fillData() {
337 // Get all of the notes from the database and create the item list
338 Cursor c = mDbHelper.fetchAllNotes();
339 startManagingCursor(c);
340
341 String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
342 int[] to = new int[] { R.id.text1 };
343
344 // Now create an array adapter and set it to display using our row
345 SimpleCursorAdapter notes =
346 new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
347 setListAdapter(notes);
348}