· 8 years ago · Jul 22, 2018, 09:02 PM
1Cursor c=null;
2 ArrayList<String> results = new ArrayList<String>();
3 try {
4 sampleDB = this.openOrCreateDatabase
5 (SAMPLE_DB_NAME, MODE_PRIVATE, null);
6
7 sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
8 SAMPLE_TABLE_NAME +
9 " (LastName VARCHAR, FirstName VARCHAR," +
10 " Country VARCHAR, Age INT(3));");
11 ContentValues values = new ContentValues();
12
13 values.put("LastName", "vinod");
14 values.put("FirstName", "sak");
15 values.put("Country","India");
16 values.put("Age","25");
17 sampleDB.insert(SAMPLE_TABLE_NAME,null,values);
18
19 c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
20 SAMPLE_TABLE_NAME +
21 " where Age > 10 LIMIT 5", null);
22
23 c.requery();
24 if (c != null ) {
25 if (c.moveToFirst()) {
26 do {
27 String firstName = c.getString(c.getColumnIndex("FirstName"));
28 int age = c.getInt(c.getColumnIndex("Age"));
29 results.add("" + firstName + ",Age: " + age);
30 }while (c.moveToNext());
31 }
32 }
33
34 // SimpleCursorAdapter sessionAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,c, from, to );
35
36 final ArrayAdapter ll=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results);
37 setListAdapter(ll);
38
39 ((ArrayAdapter<String>) ll ).notifyDataSetChanged();
40 ListView lv = getListView();
41 lv.setTextFilterEnabled(true);
42
43 lv.setOnItemClickListener(new OnItemClickListener() {
44 public void onItemClick(AdapterView<?> parent, View view,
45 int position, long id) {
46 Toast.makeText(getApplicationContext(), "hai",
47 Toast.LENGTH_SHORT).show();
48 ContentValues values = new ContentValues();
49 values.put("Age", "31");
50 sampleDB.update(SAMPLE_TABLE_NAME,values,null,null);
51 ((ArrayAdapter<String>) ll ).notifyDataSetChanged();
52 }
53 });
54
55 } catch (SQLiteException se ) {
56 Log.e(getClass().getSimpleName(), "Could not create or Open the database");
57 }