再次用到上一篇写过的db工程,
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical" tools:context=".MainActivity" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="@string/name" android:textColor="@android:color/white" android:textSize="16sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/phone" android:textColor="@android:color/white" android:textSize="16sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="@string/amount" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:padding="5dp" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textColor="@android:color/white" android:textSize="16sp" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:textColor="@android:color/white" android:textSize="16sp" /> <TextView android:id="@+id/tv_amount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout>
package cn.leigo.db; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import cn.leigo.domain.Person; import cn.leigo.service.PersonService; public class MainActivity extends Activity { private PersonService service; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); service = new PersonService(this); lv = (ListView) findViewById(R.id.lv); show(); } private void show() { } }
1.利用SimpleAdapter绑定数据
private void show() { List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); List<Person> persons = service.getScrollData(0, 20); for (Person person : persons) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("name", person.getName()); map.put("phone", person.getPhone()); map.put("amount", person.getAmount()); data.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[] { "name", "phone", "amount" }, new int[] { R.id.tv_name, R.id.tv_phone, R.id.tv_amount }); lv.setAdapter(adapter); }
2.利用SimpleCursorAdapter绑定数据,SimpleCursorAdapter绑定的数据中必须有一个名为"_id"的字段
private void show2() { Cursor c = service.getScrollDataCursor(0, 20); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, c, new String[]{"name", "phone", "amount"}, new int[]{R.id.tv_name, R.id.tv_phone, R.id.tv_amount}); lv.setAdapter(adapter); }
/** * 分页获取记录 * * @param offset跳过前面多少条记录 * @param maxResult每页获取多少条记录 * @return */ public Cursor getScrollDataCursor(int offset, int maxResult) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.rawQuery( "SELECT personid as _id,name,phone,amount FROM person order by personid asc limit ?,?", new String[] { offset + "", maxResult + "" }); return cursor; }
// 自定义适配器 private void show3() { List<Person> persons = service.getScrollData(0, 20); PersonAdapter adapter = new PersonAdapter(this, persons); lv.setAdapter(adapter); }
package cn.leigo.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import cn.leigo.db.R; import cn.leigo.domain.Person; public class PersonAdapter extends BaseAdapter { private List<Person> persons; // 绑定的数据 private LayoutInflater inflater; public PersonAdapter(Context context, List<Person> persons) { this.persons = persons; inflater = LayoutInflater.from(context); } @Override public int getCount() { return persons.size(); } @Override public Object getItem(int position) { return persons.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.item, null); holder = new ViewHolder(); holder.nameView = (TextView) convertView.findViewById(R.id.tv_name); holder.phoneView = (TextView) convertView .findViewById(R.id.tv_phone); holder.amountView = (TextView) convertView .findViewById(R.id.tv_amount); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } Person person = persons.get(position); // 实现数据绑定 holder.nameView.setText(person.getName()); holder.phoneView.setText(person.getPhone()); holder.amountView.setText(person.getAmount() + ""); return convertView; } static class ViewHolder { TextView nameView; TextView phoneView; TextView amountView; } }
实现AdapterView.OnItemClickListener接口,重写onItemClick()方法
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //1.SimpleAdapter HashMap<String, Object> map = (HashMap<String, Object>)parent.getItemAtPosition(position); String name = (String) map.get("name"); Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); //2.SimpleCursorAdapter //Cursor c = (Cursor)parent.getItemAtPosition(position); //String name = c.getString(c.getColumnIndex("name")); //Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); //3自定义适配器 //Person person = (Person) parent.getItemAtPosition(position); //Toast.makeText(this, person.getName(), Toast.LENGTH_SHORT).show(); }
parent.getItemAtPosition(position)得到的类型由public Object getItem(int position) 返回的类型决定。