· 8 years ago · Mar 31, 2018, 11:36 AM
1odb.rawquery(" delete from DATABASE_TABLE") //from memory so may not be 100%
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6
7import android.app.Activity;
8import android.database.Cursor;
9import android.os.Bundle;
10import android.view.View;
11import android.view.View.OnClickListener;
12import android.widget.Button;
13import android.widget.EditText;
14import android.widget.ListView;
15import android.widget.SimpleAdapter;
16
17
18public class MainActivity extends Activity {
19
20private ListView list_lv;
21private EditText col2_ed;
22private Button sub_btn;
23Button del_btn;
24private DBclass db;
25
26private ArrayList<String> collist_2;
27
28@Override
29public void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.activity_main);
32 collist_2 = new ArrayList<String>();
33 items();
34 getData();
35 DeleteData();
36}
37
38private void items() {
39 sub_btn = (Button) findViewById(R.id.submit_btn);
40 del_btn = (Button) findViewById(R.id.delete_btn);
41 col2_ed = (EditText) findViewById(R.id.ed2);
42 list_lv = (ListView) findViewById(R.id.dblist);
43
44
45 sub_btn.setOnClickListener(new OnClickListener() {
46
47 @Override
48 public void onClick(View v) {
49 submitData();
50 }
51 });
52
53}
54
55public void DeleteData(){
56 del_btn.setOnClickListener(new OnClickListener() {
57 @Override
58 public void onClick(View v) {
59 db.deleteData();
60 }
61 });
62}
63
64protected void submitData() {
65 String b = col2_ed.getText().toString();
66
67 db = new DBclass(this);
68 long num;
69 db.open();
70 num = db.insertmaster(b);
71 db.close();
72 getData();
73}
74
75public void getData() {
76
77 collist_2.clear();
78 db = new DBclass(this);
79 try {
80 db.open();
81 Cursor cur = db.getAllTitles();
82 while (cur.moveToNext()) {
83 String valueofcol2 = cur.getString(2);
84 collist_2.add(valueofcol2);
85 }
86 }
87 finally {
88 db.close();
89 }
90 printList();
91 setDataIntoList();
92}
93
94private void printList() {
95 for (int i = 0; i < collist_2.size(); i++) {
96 }
97}
98
99private void setDataIntoList() {
100 // create the list item mapping
101 String[] from = new String[] { "col_2" };
102 int[] to = new int[] { R.id.col2tv };
103
104 // prepare the list of all records
105 List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
106 for (int i = 0; i < collist_2.size(); i++) {
107 HashMap<String, String> map = new HashMap<String, String>();
108
109 map.put("col_2", collist_2.get(i));
110 fillMaps.add(map);
111 }
112
113 // fill in the grid_item layout
114 SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
115 R.layout.custom, from, to);
116 list_lv.setAdapter(adapter);
117}
118}
119
120import android.content.ContentValues;
121import android.content.Context;
122import android.database.Cursor;
123import android.database.SQLException;
124import android.database.sqlite.SQLiteDatabase;
125import android.database.sqlite.SQLiteOpenHelper;
126import android.util.Log;
127
128public class DBclass {
129
130public String KEY_ROWID = "_id";
131public String KEY_COL2 = "col2";
132
133private String DATABASE_NAME = "mydb";
134private String DATABASE_TABLE = "mytable";
135private int DATABASE_VERSION = 1;
136
137private Context ourContext;
138private DbHelper dbh;
139private SQLiteDatabase odb;
140
141private String USER_MASTER_CREATE =
142 "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE+ "("
143 + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_COL2 + " VARCHAR(15) )";
144
145private class DbHelper extends SQLiteOpenHelper {
146
147 public DbHelper(Context context) {
148 super(context, DATABASE_NAME, null, DATABASE_VERSION);
149 }
150
151 @Override
152 public void onCreate(SQLiteDatabase db) {
153 db.execSQL(USER_MASTER_CREATE);
154 }
155
156 @Override
157 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
158 // if DATABASE VERSION changes
159 // Drop old tables and call super.onCreate()999
160 }
161}
162
163public DBclass(Context c) {
164 ourContext = c;
165 dbh = new DbHelper(ourContext);
166}
167
168public DBclass open() throws SQLException {
169 odb = dbh.getWritableDatabase();
170 return this;
171}
172
173public void close() {
174 dbh.close();
175}
176
177public long insertmaster(String col2) throws SQLException{
178
179 ContentValues IV = new ContentValues();
180
181 IV.put(KEY_COL2, col2);
182
183 return odb.insert(DATABASE_TABLE, null, IV);
184 // returns a number >0 if inserting data is successful
185}
186
187public void updateRow(long rowID, String col2) {
188
189 ContentValues values = new ContentValues();
190 values.put(KEY_COL2, col2);
191
192 odb.update(DATABASE_TABLE, values, KEY_ROWID + "=" + rowID, null);
193}
194
195
196public void deleteData(){
197 odb.delete(DATABASE_TABLE, null,null);
198}
199
200public Cursor getAllTitles() {
201 // using simple SQL query
202 return odb.rawQuery("select * from " + DATABASE_TABLE, null);
203}
204
205public Cursor getallCols(String id) throws SQLException {
206 Cursor mCursor = odb.query(DATABASE_TABLE, new String[] { KEY_COL2 }, null, null, null, null, null);
207 Log.e("getallcols zmv", "opening successfull");
208 return mCursor;
209}
210
211public Cursor getColsById(String id) throws SQLException {
212 Cursor mCursor = odb.query(DATABASE_TABLE, new String[] { KEY_COL2 }, KEY_ROWID + " = " + id, null, null, null, null);
213 Log.e("getallcols zmv", "opening successfull");
214 return mCursor;
215}
216}