android ListView列表显示数据
item.xml
<?xml version="1.0" encoding="utf-8"?>
<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="120dp"
android:textSize="22sp"
android:layout_height="wrap_content"
android:id="@+id/name"
/> <TextView
android:layout_width="150dp"
android:textSize="22sp"
android:layout_height="wrap_content"
android:id="@+id/phone"
/> <TextView
android:layout_width="fill_parent"
android:textSize="22sp"
android:layout_height="wrap_content"
android:id="@+id/amount"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private ListView listView;
private PersonService personService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
personService = new PersonService(this);
listView = (ListView) this.findViewById(R.id.listView);
listView.setOnItemClickListener(new ItemClickListener());
show2();
}
private final class ItemClickListener implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView lView = (ListView)parent;
/* 自定义适配器
Person person = (Person) lView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), person.getId().toString(), 1).show();*/
Cursor cursor = (Cursor) lView.getItemAtPosition(position);
int personid = cursor.getInt(cursor.getColumnIndex("_id"));
Toast.makeText(getApplicationContext(), personid+ "", 1).show();
}
}
//自定义适配器
private void show3() {
List<Person> persons = personService.getScrollData(0, 20);
PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
listView.setAdapter(adapter);
}
private void show2() {
//此适配器要求有一个字段为_id,不然会报错,解决方法两种,1、增加_id的字段 2、为字段指定别名
Cursor cursor = personService.getCursorScrollData(0, 20);
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);
}
private void show() {
List<Person> persons = personService.getScrollData(0, 20);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person : persons){
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("name", person.getName());
item.put("phone", person.getPhone());
item.put("amount", person.getAmount());
item.put("id", person.getId());
data.add(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);
}
}
自定义适配器:
public class PersonAdapter extends BaseAdapter {
private List<Person> persons;//在绑定的数据
private int resource;//绑定的条目界面,item.xml
private LayoutInflater inflater; //布局填充器,通过上下文得到
public PersonAdapter(Context context, List<Person> persons, int resource) {
this.persons = persons;
this.resource = resource;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@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) {
TextView nameView = null;
TextView phoneView = null;
TextView amountView = null;
//利用View的缓存
if(convertView==null){
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.nameView = nameView;
cache.phoneView = phoneView;
cache.amountView = amountView;
convertView.setTag(cache);
}else{
ViewCache cache = (ViewCache) convertView.getTag();
nameView = cache.nameView;
phoneView = cache.phoneView;
amountView = cache.amountView;
}
Person person = persons.get(position);
//下面代码实现数据绑定
nameView.setText(person.getName());
phoneView.setText(person.getPhone());
amountView.setText(person.getAmount().toString());
return convertView;
}
private final class ViewCache{
public TextView nameView;
public TextView phoneView;
public TextView amountView;
}
}
android ListView列表显示数据的更多相关文章
- Android ListView绑定数据
ListView绑定数据的三层: ListView绑定数据源从逻辑上可以分为三层:UI层,逻辑层,数据层. UI层:UI层即是ListView控件. 数据层:要展示到ListView控件上面的数据. ...
- android ListView内数据的动态添加与删除
main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- xamarin.android listview绑定数据及点击事件
前言 listview是用来显示数据列表的一个控件,今天给大家带来如何使用cursor进行数据绑定以及点击事件. 导读 1.如何创建一个listview 2.如何使用cursor进行绑定数据 3.li ...
- android listview 添加数据
<span style="white-space:pre"> </span>listView = (ListView) findViewById(R.id. ...
- 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示
1.首先整个程序也是采用mvc的框架 DbOpenHelper 类 package dB; import android.content.Context; import android.databas ...
- Android 根据EditText搜索框ListView动态显示数据
根据EditText搜索框ListView动态显示数据是根据需求来的,觉得这之中涉及的东西可能比较的有意思,所以动手来写一写,希望对大家有点帮助. 首先,我们来分析下整个过程: 1.建立一个layou ...
- Android在ListView滑动数据混乱
我相信做过Android应用程序开发或多或少都遇到了这个问题.或者是在ListView数据损坏幻灯片事件.要么GridView数据损坏幻灯片事件. 让我们来看看一个网友写的文章,个人感觉还不错的文章: ...
- Android之listview添加数据篇
一.ListView: 1. ListView通常有两个职责: 1.向布局填充数据 2.处理选择点击等操作 2.ListView的创建需要3个元素: 1. ListView中的每一列的View. 2. ...
- Android之ListView分页数据加载
1.效果如下: 实例如下: 上图的添加数据按钮可以换成一个进度条 因为没有数据所以我加了一个按钮添加到数据库用于测试:一般在服务器拉去数据需要一定的时间,所以可以弄个进度条来提示用户: 点击加载按 ...
随机推荐
- vue,一路走来(15)--简单投票系统
今天记录一下简单的投票系统,主要实现选中至少五张作品,并提交投票. 思路:选中作品,将作品id存入到数组里. 取消投票,则从数组中移除该作品id. 如图效果: <li v-for="( ...
- Linux使用rarcrack暴力破解RAR,ZIP,7Z压缩包
1.下载http://rarcrack.sourceforge.net/ 2.安装依赖 gcc libxml2-devel libxslt-devel 3.使用rarcrack your_encry ...
- python数字图像处理(三)边缘检测常用算子
在该文将介绍基本的几种应用于边缘检测的滤波器,首先我们读入saber用来做为示例的图像 #读入图像代码,在此之前应当引入必要的opencv matplotlib numpy saber = cv2.i ...
- lambda表达式以及stream流式api用法
https://www.cnblogs.com/aoeiuv/p/5911692.html 这篇文章讲的简单全面,记录下 kotlin一些符号的用法 https://www.cnblogs.com/l ...
- python读文件的4种方式
1.直接打开就读 with open('filepath','r') as f: for line in f: print(line) print('一行数据') 虽然f是一个文件实例,但可以通过以上 ...
- 刷题or源码链接
Hadoop权威指南的Github https://github.com/tomwhite/hadoop-book hadoopAPI http://hadoop.apache.org/docs/cu ...
- controllerweb.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- 接口代码(requests库安装)
一. 首先用cd:Scripts路径名命令,进入到python--Scripts目录下:然后键入pip install requests 进行安装,有可能会要求你升级pip,键入python -m ...
- JS中常见的几种报错类型
1.SyntaxError(语法错误) 解析代码时发生的语法错误 var 1a; //Uncaught SyntaxError: Invalid or unexpected token 变量名错误 c ...
- linux0.11内核源码——进程各状态切换的跟踪
准备工作 1.进程的状态有五种:新建(N),就绪或等待(J),睡眠或阻塞(W),运行(R),退出(E),其实还有个僵尸进程,这里先忽略 2.编写一个样本程序process.c,里面实现了一个函数 /* ...