Android数据库操作-表格显示

显示表格布局

完成后效果如下:

\

首先需要一个主布局文件main.xml

01. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.     android:layout_width="fill_parent"
03.     android:layout_height="fill_parent"
04.     android:orientation="horizontal" >
05.   
06.     <View
07.         android:layout_width="0.5px"
08.         android:layout_height="fill_parent"
09.         android:background="#B8B8B8"
10.         android:visibility="visible" />
11.   
12.     <TextView
13.         android:id="@+id/id"
14.         android:layout_width="0dip"
15.         android:layout_height="35dip"
16.         android:layout_weight="2"
17.         android:textColor="#CD3700"
18.         android:textSize="20sp" />
19.   
20.     <View
21.         android:layout_width="0.5px"
22.         android:layout_height="fill_parent"
23.         android:background="#B8B8B8"
24.         android:visibility="visible" />
25.   
26.     <TextView
27.         android:id="@+id/name"
28.         android:layout_width="0dip"
29.         android:layout_height="wrap_content"
30.         android:layout_weight="3"
31.         android:textColor="#000000"
32.         android:textSize="17sp" />
33.   
34.     <View
35.         android:layout_width="0.5px"
36.         android:layout_height="fill_parent"
37.         android:background="#B8B8B8"
38.         android:visibility="visible" />
39.   
40.     <TextView
41.         android:id="@+id/age"
42.         android:layout_width="0dip"
43.         android:layout_height="wrap_content"
44.         android:layout_weight="1"
45.         android:textColor="#000000"
46.         android:textSize="17sp" />
47.   
48.     <View
49.         android:layout_width="0.5px"
50.         android:layout_height="fill_parent"
51.         android:background="#B8B8B8"
52.         android:visibility="visible" />
53.   
54. </LinearLayout>

View起到的作用是在两列之间起到分割的作用,纵观这个布局文件,就是完成这样的工作,设置一个表头,将三个TextView放置在一个水平的线性布局中去,分别显示一列的表头,然后需要一个ListView与上述的线性布局一同放入一个垂直的线性布局中去,用来显示每一条记录。而每一条记录的显示需要我们来实现一个adapter去完成每一项的显示,下面就完成这个项的布局文件:itemlayout.xml

01. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.     android:layout_width="fill_parent"
03.     android:layout_height="fill_parent"
04.     android:orientation="horizontal" >
05.   
06.     <View
07.         android:layout_width="0.5px"
08.         android:layout_height="fill_parent"
09.         android:background="#B8B8B8"
10.         android:visibility="visible" />
11.   
12.     <TextView
13.         android:id="@+id/id"
14.         android:layout_width="0dip"
15.         android:layout_height="35dip"
16.         android:layout_weight="2"
17.         android:textColor="#CD3700"
18.         android:textSize="20sp" />
19.   
20.     <View
21.         android:layout_width="0.5px"
22.         android:layout_height="fill_parent"
23.         android:background="#B8B8B8"
24.         android:visibility="visible" />
25.   
26.     <TextView
27.         android:id="@+id/name"
28.         android:layout_width="0dip"
29.         android:layout_height="wrap_content"
30.         android:layout_weight="3"
31.         android:textColor="#000000"
32.         android:textSize="17sp" />
33.   
34.     <View
35.         android:layout_width="0.5px"
36.         android:layout_height="fill_parent"
37.         android:background="#B8B8B8"
38.         android:visibility="visible" />
39.   
40.     <TextView
41.         android:id="@+id/age"
42.         android:layout_width="0dip"
43.         android:layout_height="wrap_content"
44.         android:layout_weight="1"
45.         android:textColor="#000000"
46.         android:textSize="17sp" />
47.   
48.     <View
49.         android:layout_width="0.5px"
50.         android:layout_height="fill_parent"
51.         android:background="#B8B8B8"
52.         android:visibility="visible" />
53.   
54. </LinearLayout>

在listview中每一项的布局应该是这样的,需要View来分割每一列,然后需要TextView来显示数据的信息,这些组件之间放在一个水平的线性布局中去。

这样我们就完成了程序的主体布局。接下来我们需要一个适配器(adapter)来完成对listview中每一项的数据填入。SimpleCursorAdapter是一个简单 的适配器,可以将cursor中的每一行的记录映射到一个显示的组件上一般是TextView或者是ImageView。那我们就继承这个类来完成自己的adapter。

下面是我们的adapter它继承了SimpleCursorAdapter。

01. package com.example.gird;
02.   
03. import android.content.Context;
04. import android.database.Cursor;
05. import android.graphics.Color;
06. import android.view.View;
07. import android.view.ViewGroup;
08. import android.widget.SimpleCursorAdapter;
09.   
10. public class MySimpleCursorAdapter extends SimpleCursorAdapter {
11.   
12.     public MySimpleCursorAdapter(Context context, int layout, Cursor c,
13.             String[] from, int[] to) {
14.         super(context, layout, c, from, to);
15.     }
16.   
17.     @Override
18.     public View getView(int position, View convertView, ViewGroup parent) {
19.   
20.         View view = null;
21.         if (convertView != null) {
22.             view = convertView;
23.   
24.         } else {
25.             view = super.getView(position, convertView, parent);
26.   
27.         }
28.   
29. /*author:conowen 
30.  * date:2012.4.2 
31.  * MySimpleCursorAdapter 
32.  */
33.   
34.         int[] colors = { Color.WHITE, Color.rgb(219, 238, 244) };// RGB颜色
35.   
36.         view.setBackgroundColor(colors[position % 2]);// 每隔item之间颜色不同
37.   
38.         return super.getView(position, view, parent);
39.     }
40.   
41. }

在其中完成的主要是对getView方法的重写。position当前由不可见到可见的项的位置,convertView就是要显示的组件项,这个时候Android不会每次都去实例化一个新的view对象,而是去看在缓存中是否存在一个这样的对象,若有就直接拿来,若没有才会去实例化新的对象。而parent是告诉这些个项最终会依附在哪一个父亲组件上去(Listview)。

001. package com.example.gird;
002.   
003. import android.app.Activity;
004. import android.app.AlertDialog;
005. import android.content.ContentValues;
006. import android.content.Context;
007. import android.content.DialogInterface;
008. import android.content.DialogInterface.OnClickListener;
009. import android.database.Cursor;
010. import android.database.sqlite.SQLiteCursor;
011. import android.database.sqlite.SQLiteDatabase;
012. import android.os.Bundle;
013. import android.util.Log;
014. import android.view.LayoutInflater;
015. import android.view.View;
016. import android.view.ViewGroup;
017. import android.widget.AdapterView;
018. import android.widget.AdapterView.OnItemLongClickListener;
019. import android.widget.EditText;
020. import android.widget.ListAdapter;
021. import android.widget.ListView;
022. import android.widget.TextView;
023.   
024. public class GridActivity extends Activity {
025.   
026.     public int DB_VERSION = 1;
027.     SQLiteDatabase db;
028.     // DbHelper类在DbHelper.java文件里面创建的
029.     ListView lv;
030.   
031.     @Override
032.     public void onCreate(Bundle savedInstanceState) {
033.         super.onCreate(savedInstanceState);
034.         setContentView(R.layout.main);
035.         // 建立打开数据库
036.         db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
037.         db.execSQL("DROP TABLE IF EXISTS person");
038.         // 创建person表
039.         db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
040.   
041.         // 插入数据
042.         for (int i = 0; i < 20; i++) {
043.             Person person = new Person();
044.             person.name = "john" + i;
045.             person.age = 30 - i;
046.             db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)", new Object[] {
047.                     person.name, person.age });
048.         }
049.   
050.         lv = (ListView) findViewById(R.id.lv);
051.         updatelistview();
052.         // 添加一个长按事件
053.         lv.setOnItemLongClickListener(new OnItemLongClickListener() {
054.   
055.             @Override
056.             public boolean onItemLongClick(AdapterView<?> parent, View view,
057.                     final int position, long id) {
058.                 // 实例化一个弹出框
059.                 new AlertDialog.Builder(GridActivity.this)
060.                         .setTitle("选择操作")
061.                         .setItems(new String[] { "更新", "删除", "取消" },
062.                                 // 为弹出框上的选项添加事件
063.                                 new OnClickListener() {
064.   
065.                                     @Override
066.                                     public void onClick(DialogInterface dialog,
067.                                             int which) {
068.                                         switch (which) {
069.                                         //  表示更新内容
070.                                         case 0:
071.                                             LayoutInflater inflater = getLayoutInflater();
072.                                             // 自定义一个弹出口布局
073.                                             final View layout = inflater
074.                                                     .inflate(
075.                                                             R.layout.dialog,
076.                                                             (ViewGroup) findViewById(R.id.dialog));
077.                                             EditText nameTxt = (EditText) layout
078.                                                     .findViewById(R.id.editText1);
079.                                             TextView ageTxt = (EditText) layout
080.                                                     .findViewById(R.id.editText2);
081.                                             SQLiteCursor s_old = (SQLiteCursor) lv
082.                                                     .getItemAtPosition(position);
083.                                             final int _id_old = s_old.getInt(s_old
084.                                                     .getColumnIndex("_id"));
085.                                             final String name_old = s_old.getString(s_old
086.                                                     .getColumnIndex("name"));
087.                                             final String age_old = s_old.getString(s_old
088.                                                     .getColumnIndex("age"));
089.   
090.                                             nameTxt.setText(name_old);
091.                                             ageTxt.setText(age_old);
092.                                               
093.                                             new AlertDialog.Builder(
094.                                                     GridActivity.this)
095.                                                     .setTitle("更新")
096.                                                     .setView(layout)
097.                                                     .setPositiveButton(
098.                                                             "确定",
099.                                                             new OnClickListener() {
100.   
101.                                                                 @Override
102.                                                                 public void onClick(
103.                                                                         DialogInterface dialog,
104.                                                                         int which) {
105.                                                                     ContentValues cv = new ContentValues();
106.                                                                     String temp_name = ((EditText) layout
107.                                                                             .findViewById(R.id.editText1))
108.                                                                             .getText()
109.                                                                             .toString();
110.                                                                     String temp_age = ((EditText) layout
111.                                                                             .findViewById(R.id.editText2))
112.                                                                             .getText()
113.                                                                             .toString();
114.   
115.                                                                     cv.put("_id",
116.                                                                             _id_old);
117.                                                                     cv.put("name",
118.                                                                             temp_name);
119.                                                                     cv.put("age",
120.                                                                             temp_age);
121.   
122.                                                                     String[] id_index = { String
123.                                                                             .valueOf(_id_old) };
124.                                                                     db.update(
125.                                                                             "person",
126.                                                                             cv,
127.                                                                             "_id=?",
128.                                                                             id_index);
129.                                                                     updatelistview();
130.                                                                 }
131.                                                             })
132.                                                     .setNegativeButton("取消",
133.                                                             null).show();
134.                                             break;
135.                                         // 删除记录
136.                                         case 1:
137.                                             // getItemAtPosition()得到一个item里的数据
138.                                             SQLiteCursor s = (SQLiteCursor) lv
139.                                                     .getItemAtPosition(position);
140.                                             final int _id = s.getInt(s
141.                                                     .getColumnIndex("_id"));
142.                                             String name = s.getString(s
143.                                                     .getColumnIndex("name"));
144.                                             Log.i("id ::", _id + "");
145.                                             new AlertDialog.Builder(
146.                                                     GridActivity.this)
147.                                                     .setTitle(
148.                                                             "确定删除" + name
149.                                                                     + "吗?")
150.                                                     .setPositiveButton(
151.                                                             "确定",
152.                                                             new OnClickListener() {
153.   
154.                                                                 @Override
155.                                                                 public void onClick(
156.                                                                         DialogInterface dialog,
157.                                                                         int which) {
158.                                                                     db.execSQL(
159.                                                                             "delete from person where _id =?",
160.                                                                             new Integer[] { _id });
161.                                                                     updatelistview();
162.                                                                 }
163.                                                             })
164.                                                     .setNegativeButton(
165.                                                             "取消",
166.                                                             new OnClickListener() {
167.   
168.                                                                 @Override
169.                                                                 public void onClick(
170.                                                                         DialogInterface dialog,
171.                                                                         int which) {
172.                                                                 }
173.                                                             }).show();
174.                                             break;
175.                                         // 取消操作
176.                                         case 2:
177.                                             break;
178.                                         }
179.                                     }
180.                                 }).show();
181.   
182.                 return false;
183.             }
184.   
185.         });
186.     }
187.   
188.     // 更新listview
189.     public void updatelistview() {
190.         Cursor cr = db.query("person", null, null, null, null, null, null);
191.   
192.         String id = cr.getColumnName(0);
193.         String name = cr.getColumnName(1);
194.         String age = cr.getColumnName(2);
195.         String[] ColumnNames = { id, name, age };
196.   
197.         ListAdapter adapter = new MySimpleCursorAdapter(this,
198.                 R.layout.listviewlayout, cr, ColumnNames, new int[] { R.id.id,
199.                         R.id.name, R.id.age });
200.   
201.         lv.setAdapter(adapter);
202.     }
203.   
204.     @Override
205.     protected void onPause() {
206.         onDestroy();
207.         Log.i("message", "数据库连接销毁");
208.         super.onPause();
209.     }
210.   
211.     @Override
212.     protected void onDestroy() {// 关闭数据库
213.         super.onDestroy();
214.         if (db != null) {
215.             db.close();
216.         }
217.     }
218.   
219. }

最后的效果图

\

 \

\

 对话框的布局文件代码

01. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.     android:layout_width="fill_parent"
03.     android:layout_height="fill_parent"
04.     android:orientation="horizontal" 
05.     android:id="@+id/dialog"
06.     >
07.   
08.     <RelativeLayout
09.         android:layout_width="match_parent"
10.         android:layout_height="match_parent" >
11.   
12.         <TextView
13.             android:id="@+id/textView1"
14.             android:layout_width="wrap_content"
15.             android:layout_height="wrap_content"
16.             android:layout_alignParentLeft="true"
17.             android:layout_alignParentTop="true"
18.             android:text="新姓名" />
19.   
20.         <EditText
21.             android:id="@+id/editText1"
22.             android:layout_width="wrap_content"
23.             android:layout_height="wrap_content"
24.             android:layout_alignParentRight="true"
25.             android:layout_alignParentTop="true"
26.             android:layout_marginRight="16dp"
27.             android:ems="10" />
28.   
29.         <EditText
30.             android:id="@+id/editText2"
31.             android:layout_width="wrap_content"
32.             android:layout_height="wrap_content"
33.             android:layout_alignBottom="@+id/textView2"
34.             android:layout_alignLeft="@+id/editText1"
35.             android:ems="10" />
36.   
37.         <TextView
38.             android:id="@+id/textView2"
39.             android:layout_width="wrap_content"
40.             android:layout_height="wrap_content"
41.             android:layout_alignParentLeft="true"
42.             android:layout_below="@+id/editText1"
43.             android:layout_marginTop="39dp"
44.             android:text="新年龄" />
45.   
46.     </RelativeLayout>
47.   
48. </LinearLayout
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值