本文实例讲述了Android编程使用ListView实现数据列表显示的方法。分享给大家供大家参考,具体如下:
要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:
第一种是用SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型)
第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)
显示效果如图所示:

界面布局:
item.xml
<?xml version="1.0" encoding="utf-8"?> <!--item --> <LinearLayout xmlns:android="http://schemas.android.com/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>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service = new PersonService(this);
ListView listView = (ListView) this.findViewById(R.id.listView);
//获取到集合数据
List<Person> persons = service.getScrollData(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", person.getId());
item.put("name", person.getName());
item.put("phone", person.getPhone());
item.put("amount", person.getAmount());
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>) listView.getItemAtPosition(position);
String personid = data.get("id").toString();
Toast.makeText(getApplicationContext(), personid, 1).show();
}
}
}
使用SimpleCursorAdapter进行数据绑定
public class MainActivity extends Activity {
private PersonService service;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service = new PersonService(this);
ListView listView = (ListView) this.findViewById(R.id.listView);
//获取游标
Cursor cursor = service.getCursorScrollData(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) listView.getItemAtPosition(position);
String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));
Toast.makeText(getApplicationContext(), personid, 1).show();
}
}
}
注意:使用第二种方式在获取数据集合时必须指定主键"_id"
希望本文所述对大家Android程序设计有所帮助。
本文向大家介绍Android编程实现ListView内容无限循环显示的方法,包括了Android编程实现ListView内容无限循环显示的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程实现ListView内容无限循环显示的方法。分享给大家供大家参考,具体如下: 其实要达到无限循环显示,主要就是实现继承Adapter的类。 我这里用到的是BaseAdapter 在on
本文向大家介绍Android编程实现动态更新ListView的方法,包括了Android编程实现动态更新ListView的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程实现动态更新ListView的方法。分享给大家供大家参考,具体如下: 有时候我们需要修改已经生成的列表,添加或者修改数据,notifyDataSetChanged()可以在修改适配器绑定的数组后,不用
本文向大家介绍Android ListView列表视图的使用方法,包括了Android ListView列表视图的使用方法的使用技巧和注意事项,需要的朋友参考一下 前言 当你要将某个从数据库或者文件中获得相当大的数据,在界面中向用户展示的时候,由于定义一个个视图比较麻烦,Android中提供了类似于数组的控件–ListView。 使用方法: 假设我们要转的数据是一个Person对象数组 1、首先,
本文向大家介绍android listview实现新闻列表展示效果,包括了android listview实现新闻列表展示效果的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了android listview列表展示效果的具体代码,供大家参考,具体内容如下 1.封装一些新闻数据 2.使用listview展示出来 3.设置条目点击事件,点击后跳转浏览器查看新闻 listview_item
本文向大家介绍Android编程实现在底端显示选项卡的方法,包括了Android编程实现在底端显示选项卡的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程实现在底端显示选项卡的方法。分享给大家供大家参考,具体如下: 1.layout 文件 2.Java 文件 效果如下: 更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《
问题内容: 我想要一个带有“人”名称的ListView,但是在启动应用程序时,在ListView上,我看到了一些奇怪的字符串而不是(字符串)名称。我认为这是“人”对象的强制问题。我不知道ListView应该是ListView还是ListView。因为当我尝试在listview上设置项目时,使用setItems(ObservableList)可以得到显示在列表上的怪异字符串。.这是代码: MainA
本文向大家介绍Android编程开发实现TextView显示表情图像和文字的方法,包括了Android编程开发实现TextView显示表情图像和文字的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程开发实现TextView显示表情图像和文字的方法。分享给大家供大家参考,具体如下: 从这个案例中我们可以学到当我们美化图片美化界面的时候可以在某一区域输入图片和文字混搭信息