应用截图:
一:ListView的布局文件,main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:text="@string/name" android:layout_width="120dp" android:layout_height="25dp" /> <TextView android:text="@string/phone" android:layout_width="150dp" android:layout_height="25dp" /> <TextView android:text="@string/amount" android:layout_width="fill_parent" android:layout_height="25dp" /> </LinearLayout> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="120dp" android:layout_height="25dp" /> <TextView android:id="@+id/phone" android:layout_width="150dp" android:layout_height="25dp" /> <TextView android:id="@+id/amount" android:layout_width="fill_parent" android:layout_height="25dp" /> </LinearLayout>
package com.zizhu.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { public static final String DATABASE = "user.db"; public DBOpenHelper(Context context) { super(context, DATABASE, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table t_user (id integer primary key autoincrement, name varchar(20), phone varchar(11), amount integer)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.zizhu.service; import java.util.ArrayList; import java.util.List; import com.zizhu.db.DBOpenHelper; import com.zizhu.model.User; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; public class UserService { private DBOpenHelper openHelper = null; public UserService(Context context) { openHelper = new DBOpenHelper(context); } //分页查询 public List<User> getUsers(int over, int count){ List<User> users = new ArrayList<User>(); Cursor cursor = openHelper.getReadableDatabase().rawQuery("select id, name, phone, amount from t_user limit ?, ?", new String[]{over+"", count + ""}); while(cursor.moveToNext()){ User u = new User(); u.setId(cursor.getInt(cursor.getColumnIndex("id"))); u.setName(cursor.getString(cursor.getColumnIndex("name"))); u.setPhone(cursor.getString(cursor.getColumnIndex("phone"))); u.setAmount(cursor.getInt(cursor.getColumnIndex("amount"))); users.add(u); } return users; } //添加 public void save(User user){ ContentValues values = new ContentValues(); values.put("name", user.getName()); values.put("phone", user.getPhone()); values.put("amount", user.getAmount()); openHelper.getWritableDatabase().insert("t_user", null, values); } //分页查询,返回游标 public Cursor getCursorData(int over, int count) { return openHelper.getReadableDatabase().rawQuery("select id _id, name, phone, amount from t_user limit ?, ?", new String[]{over+"", count + ""}); } }
package com.zizhu.listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.zizhu.model.User; import com.zizhu.service.UserService; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.SimpleCursorAdapter; public class MainActivity extends Activity { private ListView view = null; private UserService userService = null; private final static String [] columns = {"name", "phone", "amount"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); view = (ListView)findViewById(R.id.listView); userService = new UserService(this.getApplicationContext()); // initData(); initData2(); } private void initData() { List<User> users = userService.getUsers(0, 10); List<HashMap<String,Object>> datas = new ArrayList<HashMap<String, Object>>(); for(User u : users){ HashMap<String, Object> map = new HashMap<String, Object>(); map.put(columns[0], u.getName()); map.put(columns[1], u.getPhone()); map.put(columns[2], u.getAmount()); datas.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, datas, R.layout.item, columns, new int[]{R.id.name, R.id.phone, R.id.amount}); view.setAdapter(adapter); } private void initData2() { //使用这个类,必须使用"_id"列,如果不存在此列,可以使用别名指定这个列 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.item, userService.getCursorData(0, 10), columns, new int[]{R.id.name, R.id.phone, R.id.amount}); view.setAdapter(adapter); } }