/********************************************************************************************
* author:conowen@大钟
* E-mail:conowen@hotmail.com
* http://blog.csdn.net/conowen
* 注:本文为原创,仅作为学习交流使用,转载请标明作者及出处。
********************************************************************************************/
1、listview与GridView
其实Android本身是有表格控件(GridView)的,但是GridView的每一列的宽度被限定为一样宽,有时设计表格时,列宽不可能为同一宽度,所有可以用ListView控件去实现表格。
2、设计思路:
listview的每一列都是由一个textview去实现,表格的竖线可以通过view控件来绘制。listview每一列的颜色相互不同可以通过复写Adapter的类,然后复写getview方法具体去实现。
3、先看一下效果图:
4、数据库操作部分可以看我之前的博文:http://blog.csdn.net/conowen/article/details/7306545
本文主要在于竖线的绘制与getview方法的复写。
代码的目录结构如下图所示:
竖线的绘制:
在ListView的布局文件中,每隔一个TextView,就增加一个<View>控件。就是绘制一条竖线的意思。可以设置表格竖线的长度,宽度,颜色等等。
颜色的交互:
首先要知道listview的工作原理,每次得到一个item,listview都会通过getview来绘制一个item,在getview方法中,可以设置这个item的各种属性,如颜色,布局等等。
1 | public View getView( final int position, View convertView, ViewGroup parent){ |
5、详细代码:
05 | package com.conowen.grid; |
07 | import android.app.Activity; |
08 | import android.database.Cursor; |
09 | import android.database.sqlite.SQLiteDatabase; |
10 | import android.os.Bundle; |
11 | import android.widget.ListAdapter; |
12 | import android.widget.ListView; |
14 | public class GridActivity extends Activity { |
17 | public String DB_NAME = "DB.sqlite" ; |
18 | public String DB_TABLE = "num" ; |
19 | public int DB_VERSION = 1 ; |
20 | final DataHelper helper = new DataHelper( this , DB_NAME, null , DB_VERSION); |
25 | public void onCreate(Bundle savedInstanceState) { |
27 | super .onCreate(savedInstanceState); |
28 | setContentView(R.layout.main); |
29 | sqldb = helper.getWritableDatabase(); |
30 | lv = (ListView) findViewById(R.id.lv); |
35 | public void updatelistview() { |
38 | Cursor cr = sqldb.query( "JobChecker" , null , null , null , null , null , |
41 | String id = cr.getColumnName( 0 ); |
42 | String job = cr.getColumnName( 2 ); |
43 | String address = cr.getColumnName( 4 ); |
44 | String student = cr.getColumnName( 5 ); |
45 | String[] ColumnNames = { id, job, address, student }; |
47 | ListAdapter adapter = new MySimpleCursorAdapter( this , |
48 | R.layout.listviewlayout, cr, ColumnNames, new int [] { R.id.id, |
49 | R.id.job, R.id.addr, R.id.student }); |
53 | lv.setAdapter(adapter); |
58 | protected void onDestroy() { |
05 | package com.conowen.grid; |
07 | import android.content.Context; |
08 | import android.database.Cursor; |
09 | import android.graphics.Color; |
10 | import android.view.View; |
11 | import android.view.ViewGroup; |
12 | import android.widget.SimpleCursorAdapter; |
14 | public class MySimpleCursorAdapter extends SimpleCursorAdapter { |
16 | public MySimpleCursorAdapter(Context context, int layout, Cursor c, |
17 | String[] from, int [] to) { |
18 | super (context, layout, c, from, to); |
24 | public View getView( final int position, View convertView, ViewGroup parent) { |
29 | if (convertView != null ) { |
36 | view = super .getView(position, convertView, parent); |
40 | int [] colors = { Color.WHITE, Color.rgb( 219 , 238 , 244 ) }; |
42 | view.setBackgroundColor(colors[position % 2 ]); |
44 | return super .getView(position, view, parent); |
05 | package com.conowen.grid; |
07 | import android.content.Context; |
08 | import android.database.sqlite.SQLiteDatabase; |
09 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
10 | import android.database.sqlite.SQLiteOpenHelper; |
12 | public class DataHelper extends SQLiteOpenHelper { |
15 | public synchronized void close() { |
20 | public DataHelper(Context context, String name, CursorFactory factory, |
22 | super (context, name, factory, version); |
28 | public void onCreate(SQLiteDatabase db) { |
31 | String sql = "CREATE TABLE JobChecker (_id INTEGER PRIMARY KEY , department VARCHAR, job VARCHAR,teacher VARCHAR,address VARCHAR,student VARCHAR,isworking VARCHAR)" ; |
37 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
main.xml01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas./apk/res/android" |
03 | android:layout_width = "fill_parent" |
04 | android:layout_height = "fill_parent" |
05 | android:orientation = "vertical" > |
08 | xmlns:android = "http://schemas./apk/res/android" |
09 | android:layout_width = "fill_parent" |
10 | android:layout_height = "wrap_content" |
11 | android:orientation = "horizontal" > |
14 | android:layout_width = "40dip" |
15 | android:layout_height = "30dp" |
17 | android:textSize = "20sp" /> |
21 | android:layout_width = "200dip" |
22 | android:layout_height = "30dp" |
24 | android:textSize = "20sp" /> |
27 | android:id = "@+id/addr" |
28 | android:layout_width = "150dip" |
29 | android:layout_height = "30dp" |
31 | android:textSize = "20sp" /> |
34 | android:id = "@+id/student" |
35 | android:layout_width = "100dip" |
36 | android:layout_height = "30dp" |
38 | android:textSize = "20sp" /> |
41 | android:id = "@+id/isworking" |
42 | android:layout_width = "80dip" |
43 | android:layout_height = "30dp" |
45 | android:textSize = "20sp" /> |
50 | android:layout_width = "fill_parent" |
51 | android:layout_height = "wrap_content" > |
listviewlayout.xml01 | < LinearLayout xmlns:android = "http://schemas./apk/res/android" |
02 | android:layout_width = "fill_parent" |
03 | android:layout_height = "fill_parent" |
04 | android:orientation = "horizontal" > |
07 | android:layout_width = "0.5px" |
08 | android:layout_height = "fill_parent" |
09 | android:background = "#B8B8B8" |
10 | android:visibility = "visible" /> |
14 | android:layout_width = "40dip" |
15 | android:layout_height = "55dip" |
16 | android:textColor = "#CD3700" |
17 | android:textSize = "20sp" /> |
20 | android:layout_width = "0.5px" |
21 | android:layout_height = "fill_parent" |
22 | android:background = "#B8B8B8" |
23 | android:visibility = "visible" /> |
27 | android:layout_width = "200dip" |
28 | android:layout_height = "wrap_content" |
29 | android:textColor = "#000000" |
30 | android:textSize = "17sp" /> |
33 | android:layout_width = "0.5px" |
34 | android:layout_height = "fill_parent" |
35 | android:background = "#B8B8B8" |
36 | android:visibility = "visible" /> |
39 | android:id = "@+id/addr" |
40 | android:layout_width = "150dip" |
41 | android:layout_height = "wrap_content" |
42 | android:textColor = "#000000" |
43 | android:textSize = "17sp" /> |
46 | android:layout_width = "0.5px" |
47 | android:layout_height = "fill_parent" |
48 | android:background = "#B8B8B8" |
49 | android:visibility = "visible" /> |
52 | android:id = "@+id/student" |
53 | android:layout_width = "100dip" |
54 | android:layout_height = "wrap_content" |
55 | android:textColor = "#000000" |
56 | android:textSize = "20sp" /> |
59 | android:layout_width = "0.5px" |
60 | android:layout_height = "fill_parent" |
61 | android:background = "#B8B8B8" |
62 | android:visibility = "visible" /> |
|