Android编程使⽤ListView实现数据列表显⽰的⽅法
本⽂实例讲述了Android编程使⽤ListView实现数据列表显⽰的⽅法。分享给⼤家供⼤家参考,具体如下:
要将数据库中的数据列表显⽰在屏幕上,我们要使⽤ListView这个控件,当⽤户从数据库中取出数据时,要将数据绑定到显⽰控件上,如何绑定呢,我们需要创建适配器进⾏绑定,创建适配器有两种⽅式:
第⼀种是⽤SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型)
第⼆种是⽤SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)
显⽰效果如图所⽰:
界⾯布局:
<?xml version="1.0" encoding="utf-8"?>
<!--item -->
<LinearLayout
xmlns:android="schemas.android/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 名称 -->
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="@+id/name"
/
>
<!-- 电话 -->
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/phone"
/>
<!-- 存款 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/amount"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
android编程入门指南 pdf
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 标题 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:text="姓名"
/>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="电话"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="存款"
/>
</LinearLayout>
<!-- ListView控件 -->
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"
/>
</LinearLayout>
使⽤SimpleAdapter进⾏数据绑定
public class MainActivity extends Activity {
private PersonService service;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
service = new PersonService(this);
ListView listView = (ListView) this.findViewById(R.id.listView);
//获取到集合数据
List<Person> persons = ScrollData(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("id", Id());
item.put("name", Name());
item.put("phone", Phone());
item.put("amount", Amount());
data.add(item);
}
//创建SimpleAdapter适配器将数据绑定到item显⽰控件上
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,
new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});
//实现列表的显⽰
listView.setAdapter(adapter);
//条⽬点击事件
listView.setOnItemClickListener(new ItemClickListener());
}
/
/获取点击事件
private final class ItemClickListener implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView listView = (ListView) parent;
HashMap<String, Object> data = (HashMap<String, Object>) ItemAtPosition(position);      String personid = ("id").toString();
Toast.makeText(getApplicationContext(), personid, 1).show();
}
}
}
使⽤SimpleCursorAdapter进⾏数据绑定
public class MainActivity extends Activity {
private PersonService service;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
service = new PersonService(this);
ListView listView = (ListView) this.findViewById(R.id.listView);
//获取游标
Cursor cursor = CursorScrollData(0, 10);
//创建SimpleCursorAdapter适配器将数据绑定到item显⽰控件上
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);
//条⽬点击事件
listView.setOnItemClickListener(new ItemClickListener());
}
private final class ItemClickListener implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView listView = (ListView) parent;
Cursor cursor = (Cursor) ItemAtPosition(position);
String personid = String.ColumnIndex("_id")));
Toast.makeText(getApplicationContext(), personid, 1).show();
}
}
}
注意:使⽤第⼆种⽅式在获取数据集合时必须指定主键"_id"希望本⽂所述对⼤家Android程序设计有所帮助。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。