一、界面设计:

  1、activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/name" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/age" />
</LinearLayout> <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp" > </ListView> </RelativeLayout>

  2、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/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

  3、最终测试界面:

  每行显示用户名称和年龄。

二、三种方式将数据绑定到ListView控件上:

package com.example.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; import com.example.entity.Person;
import com.example.lservice.DBPersonService;
import com.example.lservice.PersonAdapter; public class MainActivity extends Activity { private ListView listView;
private DBPersonService personService; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) this.findViewById(R.id.listView);
personService = new DBPersonService(this);
     //监听行点击事件
listView.setOnItemClickListener(new OnItemClickListener() {
//parent:指当前被点击条目的listview view:指当前被点击的条目 position:被点击条目的索引
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView lv = (ListView) parent;
/*
* 1、自定义适配器当前条目是Person对象。
* 2、采用SimpleCursorAdapter适配器,返回的是Cursor对象。
* 3、采用SimpleAdapter适配器,返回的是HashMap<String, Object>对象。
*/
Person p = (Person) lv.getItemAtPosition(position);
String info = p.getName() + " " + position + " " + id;
Toast.makeText(getApplicationContext(), info, 1).show(); } });
show3();
} //第一种: 使用SimpleAdapter作为适配器
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> map = new HashMap<String, Object>();
map.put("name", person.getName());
map.put("age", person.getAge());
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.item,// 就是一个界面显示文件item.xml
new String[] { "name", "age" },//from: 从结果集中获取数据,将需要显示的数据列名放到该集合
new int[] { R.id.name, R.id.age }// to:将结果集的数据字段对应到listview(item.xml)上
);
listView.setAdapter(adapter);
} //第二种: 使用SimpleCursorAdapter作为适配器
private void show2() {
Cursor cursor = personService.getCursorScrollData(0, 20);
// 使用SimpleCursorAdapter适配器注意两个问题:
// 1、返回的游标对象不能被关闭。
// 2、查询语句字段必须有"_id"这个字段,例如:select id as _id,name,age from person。
@SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, new String[] { "name", "age" },
new int[] { R.id.name, R.id.age });
listView.setAdapter(adapter);
} //第三种: 自定义适配器PersonAdapter
private void show3() {
List<Person> persons = personService.getScrollData(0, 10);
PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
listView.setAdapter(adapter);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

  自定义适配器类实现:

package com.example.lservice;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; import com.example.entity.Person;
import com.example.listview.R; public class PersonAdapter extends BaseAdapter {
//被绑定的数据
private List<Person> persons;
//绑定条目界面
private int resource;
//布局填充,作用是可以用xml文件生成View对象
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;
} //会缓存之前的View
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView nameView =null;
TextView ageView = null;
if (convertView == null) {// 表示是第一页,需要创建该对象
//生成条目界面对象
convertView = inflater.inflate(resource, null);
nameView = (TextView) convertView.findViewById(R.id.name);
ageView = (TextView) convertView.findViewById(R.id.age);
ViewCache cache = new ViewCache();
cache.ageView=ageView;
cache.nameView=nameView;
//Tags can also be used to store data within a view without resorting to another data structure.
convertView.setTag(cache);
}else{
ViewCache cache = (ViewCache) convertView.getTag();
nameView = cache.nameView;
ageView = cache.ageView;
}
// TextView nameView = (TextView) convertView.findViewById(R.id.name);
// TextView ageView = (TextView) convertView.findViewById(R.id.age);
Person person = persons.get(position);
nameView.setText(person.getName());
//setText方法:被设置的值要转换成字符串类型,否则出错
ageView.setText(person.getAge().toString()); return convertView;
}
/**
* 由于每次都要查找View对象,因此就通过setTag将其保存起来。
* @author Administrator
*
*/
private final class ViewCache{
public TextView nameView;
public TextView ageView; }
}

Android学习笔记_11_ListView控件使用的更多相关文章

  1. [Android学习笔记]组合控件的使用

    组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...

  2. android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)

    DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...

  3. 十三、Android学习笔记_Andorid控件样式汇总

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  4. Android学习笔记_75_Andorid控件样式汇总

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  5. Android学习笔记_57_ExpandableListView控件应用

    1.布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  6. android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件

    主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...

  7. android菜鸟学习笔记14----Android控件(三) ListView的简单使用

    MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...

  8. android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例

    有时候,可能觉得系统提供的控件太丑,就会需要自定义控件来实现自己想要的效果. 以下主要参考<第一行代码> 1.自定义一个标题栏: 系统自带的标题栏很丑,且没什么大的作用,所以我们之前会在o ...

  9. iOS学习笔记——基础控件(上)

    本篇简单罗列一下一些常用的UI控件以及它们特有的属性,事件等等.由于是笔记,相比起来不会太详细 UIView 所有UI控件都继承于这个UIView,它所拥有的属性必是所有控件都拥有,这些属性都是控件最 ...

随机推荐

  1. hibernate框架配置文件详解

    1 orm元数据配置文件(映射文件) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hib ...

  2. 【python爬虫】用python编写LOL战绩查询

    介绍一个简单的python爬虫,通过Tkinter创建一个客户端,当输入要查询的LOL用户名称的时候,可以显示出当前用户的所在服务器,当前战力和当前段位. 爬取网页地址:http://lol.duow ...

  3. [Activator-HelloAkka] Create our Actors

    So far we have defined our Actor and its messages. Now let's create an instance of this actor. In Ak ...

  4. pat1007. Maximum Subsequence Sum (25)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. 【Linux】time+dd测试硬盘读写速度

    dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. dd 命令通用语法格式如下: dd if=path/to/input_file ...

  6. TD不换行 nowrap属性

    表格table的td单元格中,文字长了往往会撑开单元格,但是如果table都不够宽了,就换行了好像(不要较真其他情况,我只说会换行的情况).换行后的表格显得乱糟糟,不太好看,我不喜欢这样的换行.当然可 ...

  7. markdown语法简单总结

    最常用的十个MarkDown语法总结: 标题:只要在这段文字前加 # 号即可 # 一级标题 最大 ## 二级标题 ### 三级标题 无序列表:在文字前加上 - 或 * 有序列表:在文字前加1. 2.  ...

  8. 实用的随机数生成类Random:测试(随机产生100个不重复的正整数)

    实用的随机数生成类Random:测试(使用Random类随机生成100个不重复的正整数) 一.之前我们使用随机数用的是Math类的random()方法: tips: 产生随机数(0~9中任意整数)的方 ...

  9. Hibernate中的session的线程安全问题

    SessionFactory的实现是线程安全的,多个并发的线程可以同时访问一 个SessionFactory并从中获取Session实例, 而Session不是线程安全的,Session中包含了数 据 ...

  10. 对 Vue 的理解(一)

    一.什么是 Vue ? 首先,Vue 是一个 MVVM 框架,M -- 模型,就是用来定义驱动的数据,V -- 视图,是经过数据改变后的 html,VM -- 框架视图,就是用来实现双向绑定的中间桥梁 ...