main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="70dp" android:layout_height="wrap_content" android:text="编号" android:textSize="20sp" android:gravity="center" android:textStyle="bold" /> <TextView android:layout_width="190dp" android:layout_height="wrap_content" android:text="姓名" android:textSize="20sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> <ListView //这里的数据用数据库里的数据填充 android:id="@+id/lvPerson" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollingCache="false" //如果设为true,当拖动listView,会出现黑色背景 android:background="@drawable/bg" android:divider="@drawable/line"//线条 ></ListView> </LinearLayout>
Activity:
public class ListView2Activity extends Activity {
private ListView lvPerson; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lvPerson=(ListView)findViewById(R.id.lvPerson); PersonDAO pDao=new PersonDAO(this); Cursor cursor=pDao.getPersons(); //得到PersonDAO对象中的返回的cursor集合 SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.person_item,cursor,new String[]{"_id","pname","pgender"},new int[]{R.id.tvPid,R.id.tvPname,R.id.ivPgender}); //SimpleCursorAdapter 有三个字段 context,layout布局文件,要显示的表列名,把要显示的列名数据填充到指定控件中 lvPerson.setAdapter(adapter); } }
注意这里并没有继承listActivity
person_item.xml
填充数据的布局文件(填充到listview控件中)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tvPid" android:layout_width="70dp" android:layout_height="50dp" android:gravity="center" android:textSize="15sp" /> <TextView android:id="@+id/tvPname" android:layout_width="190dp" android:layout_height="50dp" android:gravity="center" android:textSize="15sp" /> <TextView android:id="@+id/ivPgender" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:textSize="15sp" /> </LinearLayout>
DBOPenHelper
public class DBOPenHelper extends SQLiteOpenHelper { private static final int VERSION = 1; private static final String DBNAME = "data.db"; private static final String PERSON="t_person"; public DBOPenHelper(Context context) { super(context, DBNAME, null, VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { //如果用listView必须用一个_id的字段 db.execSQL("create table "+PERSON+" (_id varchar(4) primary key,pname varchar(20),pgender varchar(2))"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1001','张三','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1002','李四','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1003','王五','女')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1004','赵钱','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1005','孙李','女')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1006','周吴','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1007','郑王','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1008','冯陈','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1009','褚卫','女')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1010','蒋沈','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1011','韩杨','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1012','朱秦','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1013','尤许','男')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
PersonDAO
public class PersonDAO { private DBOPenHelper helper; private SQLiteDatabase db; public PersonDAO(Context context) { helper = new DBOPenHelper(context); } //分页 public Cursor getPersons(int start, int count) { db = helper.getWritableDatabase(); Cursor cursor=db.query("t_person", new String[]{"_id","pname","pgender"}, null, null, null, null, "_id desc",start+","+count); //db.query("表名", 表字段, null, 条件(_id=1), 条件对象(new String[] {pname}), gruop by 分组字段, "order by的字段(_id desc)",分页索引,分页的总数据); return cursor; } public Cursor getPersons() { db = helper.getWritableDatabase(); Cursor cursor=db.query("t_person", new String[]{"_id,pname,pgender"}, null, null, null, null, null); return cursor; } public long getCount() { db = helper.getWritableDatabase(); Cursor cursor = db.rawQuery("select count(_id) from t_person", null); if (cursor.moveToNext()) { return cursor.getLong(0); } return 0; } }