ListView 页面:
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textId" >
</ListView>
模板界面(及每行显示的内容为):
<TextView
android:id="@+id/textName"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textSize="17px"
android:text="姓 名" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textPhone"
android:layout_width="234dp"
android:layout_height="wrap_content"
android:text="电话号"
android:textSize="15px" />
<TextView
android:id="@+id/textId"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="序号" />
</LinearLayout>
最后程序样式:
必须的变量:
private personservice ps; //数据操作对象(增删改查)
private ListView listView; //列表框对象
List<person> listper; //在方法一种用于装置数据,点击事件后取得所选行全部对象;
初始化数据信息:
ps=new personservice(this); //数据实现部分参考SQLite的操作部分
listView=(ListView) this.findViewById(R.id.listView1); //列表框
方法一、SimpleAdapter方式绑定数据:
private void show() {
listper=ps.getPage(0, 100);
List<HashMap<String, Object>> data=new ArrayList<HashMap<String, Object>>();
for(person p :listper)
{
HashMap<String, Object> item =new HashMap<String, Object>();
item.put("id", "【"+p.getId()+"】");
item.put("name", p.getName());
item.put("phone", p.getPhone());
data.add(item);
}
SimpleAdapter adapter=new SimpleAdapter(this, data,R.layout.item,
new String[]{"id","name","phone"}, new int[]{R.id.textId,R.id.textName,R.id.textPhone});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ItemClickListener());
}
private final class ItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), listper.get(arg2).getName()+":"+listper.get(arg2).getPhone(), 1).show();
}
}
方法二、SimpleCursorAdapter方式绑定数据:
private void show2(){
Cursor cursor=ps.getPageCursor(0, 100);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, R.layout.item,cursor, new String[]{"_id","name","phone"},new int[]{R.id.textId ,R.id.textName,R.id.textPhone});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ItemClickListener2());
}
private final class ItemClickListener2 implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ListView lView=(ListView)arg0;
Cursor cursor=(Cursor) lView.getItemAtPosition(arg2);
int StrId=cursor.getInt(cursor.getColumnIndex("_id"));
Toast.makeText(getApplicationContext(), "序号为"+StrId,Toast.LENGTH_SHORT).show();
}
}
方法三、自定义方式绑定数据:定义PersonAdapter 类继承与 BaseAdapter
1)类实现:
public class PersonAdapter extends BaseAdapter {
private List<person> persons;//在绑定的数据
private int resouce;//绑定的条目界面
private LayoutInflater inflater; //系统服务,布局填充服务
public PersonAdapter(Context context, List<person> persons,int resouce) {
this.persons = persons;
this.resouce=resouce;
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();//返回总数
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return persons.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
convertView=inflater.inflate(resouce, null);
}
TextView idView=(TextView)convertView.findViewById(R.id.textId);
TextView nameView=(TextView)convertView.findViewById(R.id.textName);
TextView phoneView=(TextView)convertView.findViewById(R.id.textPhone);
person p=persons.get(position);
idView.setText("【"+p.getId()+"】");
nameView.setText(p.getName());
phoneView.setText(p.getPhone());
return convertView;
}
}
2)调用
private void show3() {
// TODO Auto-generated method stub
List<person> lp=ps.getPage(0, 100);
PersonAdapter adapter=new PersonAdapter(this,lp,R.layout.item);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ItemClickListener3());
}
private final class ItemClickListener3 implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
ListView lV=(ListView)arg0;
person p=(person)lV.getItemAtPosition(arg2);
Toast.makeText(getApplicationContext(), p.getName(),Toast.LENGTH_SHORT).show();
}
}