· 9 years ago · Dec 03, 2016, 08:25 AM
1package ting.a1203_2;
2
3import android.database.Cursor;
4import android.database.SQLException;
5import android.database.sqlite.SQLiteDatabase;
6import android.support.v7.app.AppCompatActivity;
7import android.os.Bundle;
8import android.view.View;
9import android.widget.AdapterView;
10import android.widget.Button;
11import android.widget.EditText;
12import android.widget.ListView;
13import android.widget.SimpleCursorAdapter;
14import android.widget.Toast;
15
16import static android.R.attr.id;
17
18public class MainActivity extends AppCompatActivity {
19 private SQLiteDatabase db = null;
20 private final static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS table02(_id INTEGER PRIMARY KEY, name TEXT, price INTEGER)";
21
22 ListView lv1;
23 Button btnSearchData, btnSearchAll;
24 EditText etID;
25 Cursor cursor;
26
27
28 @Override
29 protected void onCreate(Bundle savedInstanceState) {
30 super.onCreate(savedInstanceState);
31 setContentView(R.layout.activity_main);
32
33 lv1 = (ListView) findViewById(R.id.lv1);
34 btnSearchData = (Button) findViewById(R.id.btnSearchData);
35 btnSearchAll = (Button) findViewById(R.id.btnSearchAll);
36 etID = (EditText) findViewById(R.id.etID);
37
38 btnSearchData.setOnClickListener(myListener);
39 btnSearchAll.setOnClickListener(myListener);
40 lv1.setOnItemClickListener(lv1Listener);
41
42 db = openOrCreateDatabase("db02.db", MODE_PRIVATE, null); // 建立或開啟資料庫db02.db
43 // 新增資料表 æ’入資料
44 try {
45 db.execSQL(CREATE_TABLE);
46 db.execSQL("INSERT INTO table02 (name, price) values ('Macbook Air', 33234)");
47 db.execSQL("INSERT INTO table02 (name, price) values ('ASUS', 15000)");
48 db.execSQL("INSERT INTO table02 (name, price) values ('msi', 40000)");
49 db.execSQL("INSERT INTO table02 (name, price) values ('lenovo', 50000)");
50 } catch (Exception e) {}
51
52 // é è¨é¡¯ç¤ºå…¨éƒ¨
53 cursor = getAll();
54 UpdateAdapter(cursor);
55 }
56
57 private ListView.OnItemClickListener lv1Listener = new ListView.OnItemClickListener() {
58 @Override
59 public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
60 cursor.moveToPosition(position);
61 Cursor c = get(id);
62 String s = "id= " + id + "\r\n" + "name=" + c.getString(1) + "\r\n" + "price=" + c.getInt(2);
63 Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
64 }
65 };
66
67
68 @Override
69 protected void onDestroy() {
70 super.onDestroy();
71 db.close();
72 }
73
74 private Button.OnClickListener myListener = new Button.OnClickListener() {
75 @Override
76 public void onClick(View view) {
77 try {
78 switch (view.getId()) {
79 case R.id.btnSearchData:
80 {
81 long id = Integer.parseInt(etID.getText().toString());
82 cursor = get(id);
83 UpdateAdapter(cursor);
84 break;
85 }
86 case R.id.btnSearchAll:
87 {
88 cursor = getAll();
89 UpdateAdapter(cursor);
90 break;
91 }
92 }
93 } catch (Exception err) {
94 Toast.makeText(getApplicationContext(), "查無æ¤è³‡æ–™ï¼", Toast.LENGTH_LONG).show();
95 }
96 }
97 };
98
99 private void UpdateAdapter(Cursor cursor) {
100 if (cursor != null && cursor.getCount() >= 0) {
101 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.mylayout ,
102 cursor, new String[]{"_id", "name" , "price"},
103 new int[] {R.id.txt, R.id.txtName, R.id.txtPrice}, 0);
104
105 // SimpleCursorAdapter è² è²¬æ©‹æŽ¥é¡¯ç¤ºç•Œé¢å…ƒä»¶ListView與Cursor
106 // åƒæ•¸1: 本應用程å¼
107 // åƒæ•¸2: 顯示的介é¢å…ƒä»¶é…置資æºé»¨
108 // åƒæ•¸3: cursor為è¦é¡¯ç¤ºçš„資料
109 // åƒæ•¸4: è¦é¡¯ç¤ºçš„æ¬„ä½ï¼Œ_id , name, price
110 // åƒæ•¸5: 以自訂的myLayout 顯示界é¢å…ƒä»¶å°æ‡‰çš„æ¬„ä½
111 // åƒæ•¸6: 代表flag(旗標),自Android 4.X版後è¨å®šç‚º0,å¯å–得更好地執行效率。
112 lv1.setAdapter(adapter);
113 }
114 }
115
116 public Cursor getAll(){
117 Cursor cursor = db.rawQuery("SELECT _id , name , price FROM table02" , null);
118 return cursor;
119 }
120
121 public Cursor get(long rowId)throws SQLException{
122 Cursor cursor = db.rawQuery("SELECT _id , name , price FROM table02 WHERE _id = " + rowId , null);
123 if(cursor.getCount()>=0)
124 cursor.moveToFirst();
125 else
126 Toast.makeText(getApplicationContext(), "No data" , Toast.LENGTH_LONG).show();
127 return cursor;
128 }
129
130}