您的位置:首页 > 移动开发 > Android开发

Android ListView常用属性及数据绑定显示

2013-04-08 10:26 477 查看
首先,创建一个用于显示每一行item的layout,名为item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/name"
android:layout_width="120dp"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/phone"
android:layout_width="150dp"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>


然后,在main.xml中,添加一个ListView数据显示控件,添加id名称为listview

接下来便是进行数据的绑定,常用的为为三种方法,下面ps.getScrollData方法返回一个数据源集合:

private void show1() {
List persons = ps.getScollData(0, 10);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (Person person : persons) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("amount", person.getAmount());
item.put("id", person.getId());
item.put("name", person.getName());
item.put("phone", person.getPhone());
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),data, R.layout.item,
new String[] { "name", "phone", "amount" }, new int[] {R.id.name, R.id.phone, R.id.amount });
          // item表示为之前定义的item.xml,表示将data集合中的每一个对象绑定到些item中,即将data中的每一项绑定到一个视图item上;
          // 后两个参数表示把结果集中哪些key的值绑定到哪些控件上;(把结果集中key为name对应的对象绑定到视图中id为name的控件上)
listview.setAdapter(adapter);//内部处理流程如下
// {int total = adapter.getCount();// 获取得到的数据总数
// int perpage = 7;//获取每一页的显示条目数,
// for (int i = 0; i < perpage; i++) {
// View view = adapter.getView(i, convertView, parent);//第二次执行时会将前一次执行的view传给convertView;
// 显示条目
// }}
}
private void show2() {//此方法需要一个结果集中的Cursor,要求Cursor中需要有一个名称为_id的字段;所以添加一个获取Cursor的方法如下!
Cursor cursor = ps.getCursorScollData(0, 10);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.item, cursor,
new String[] { "name", "phone", "amount" }, new int[] {R.id.name, R.id.phone, R.id.amount });
listview.setAdapter(adapter);
}
     public Cursor getCursorScollData(int offest, int maxResult) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();//因为要求结果集中要有一个名为_id的字,所以SQL语句如下!
Cursor cursor = db.rawQuery("select personid as _id,name,phone,amount from person order by personid asc limit ?,?",
new String[] { String.valueOf(offest),String.valueOf(maxResult) });
// db.query(table, columns, selection, selectionArgs, groupBy, having,orderBy, limit);
return cursor;
}
private void show3() {
List persons = ps.getScollData(0, 10);
PersonAdapter adapter = new PersonAdapter(getApplicationContext(),persons, R.layout.item);
listview.setAdapter(adapter);
}


当每点击一项需要获取此项的相关信息时可以添加此方法:

listview.setOnItemClickListener(new ItemClickListener());
private final class ItemClickListener implements OnItemClickListener {
@Override//此方法中第一个参数为显示数据(item项)的控件,在此例子中即为ListView;第三个参数为点击项的位置,
public void onItemClick(AdapterView<?> parent, View arg1, int position,long arg3) {
ListView lview = (ListView) parent;
// show3()方法对应的处理方法
// Person person = (Person) lview.getItemAtPosition(position);
// Toast.makeText(getApplicationContext(),person.getId().toString(),1).show();

       // show2()方法对应的处理方法
       // show2方法中,adapter返回的是Cursor,
// Cursor cursor = (Cursor) lview.getItemAtPosition(position);
// int personid = cursor.getInt(cursor.getColumnIndex("_id"));
// Toast.makeText(getApplicationContext(), personid + "", 1).show();

       // show1()方法对应的处理方法
// show1方法中,adapter返回的是Map,再对Map进行操作取出相应的id值
HashMap<String, Object> item = (HashMap<String, Object>) lview.getItemAtPosition(position);
int personid = (Integer) item.get("id");
Toast.makeText(getApplicationContext(), personid + "", 1).show();
}
}


自定义适配器

public class PersonAdapter extends BaseAdapter {
private List persons;// 要绑定的数据
private int resource;// 绑定的一个条目界面的id,此例中即为item.xml
private LayoutInflater inflater;// 布局填充器,它可以使用一个xml文件生成一个View对象,可以通过Context获取实例对象

public PersonAdapter(Context context, List persons, int resource) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.resource = resource;
this.persons = persons;
}

@Override
public int getCount() {// 得到要绑定的数据总数
return persons.size();
}

@Override
public Object getItem(int position) {// 给定索引值,得到索引值对应的对象
return persons.get(position);
}

@Override
public long getItemId(int position) {// 获取条目id
return position;
}

// ListView有缓存功能,当显示第一页页面时会创建页面对象,显示第二页时重用第一页创建好了的对象
// 取得条目界面:position代表当前条目所要绑定的数据在集合中的索引值
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView nameView = null;
TextView phoneView = null;
TextView amountView = null;
if (convertView == null) {// 显示第一页的时候convertView为空
convertView = inflater.inflate(resource, null);// 生成条目对象
nameView = (TextView) convertView.findViewById(R.id.name);
phoneView = (TextView) convertView.findViewById(R.id.phone);
amountView = (TextView) convertView.findViewById(R.id.amount);

ViewCache cache = new ViewCache();
cache.amountView = amountView;
cache.nameView = nameView;
cache.phoneView = phoneView;
convertView.setTag(cache);
} else {
ViewCache cache = (ViewCache) convertView.getTag();
amountView = cache.amountView;
nameView = cache.nameView;
phoneView = cache.phoneView;
}

Person person = persons.get(position);
// 实现数据绑定
nameView.setText(person.getName());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount());
return convertView;
}

private final class ViewCache {
public TextView nameView;
public TextView phoneView;
public TextView amountView;
}
}


下面看看ListView的常用属性

<ListView
android:drawSelectorOnTop="false" 设置点击某一项时,点击项的颜色显示在item的上面或不是
android:drawingCacheQuality="auto"
android:cacheColorHint="#00000000" 当设置了背景颜色时,触摸lv会变黑,设置此缓存色为透明即可解决
android:fadeScrollbars="true" 设置滚动时滚动条的自动隐藏
android:transcriptMode="alwaysScroll" 设置当显示大量item的时候,希望最新的条目可以显示到可视范围,
android:fadingEdge="none" 消除上下边框的黑色的框
android:scrollbars="none" 设置为none时,将隐藏滚动条
android:fastScrollEnabled="true" 设置显示快速滚动滑块
android:stackFromBottom="true" > 自动显示到列表最底部
</ListView>


ListView默认没有选择模式, 可以设置选择模式为CHOICE_MODE_SINGLE和CHOICE_MODE_MUTIPLE;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐