ListView绑定数据的三层:

ListView绑定数据源从逻辑上可以分为三层:UI层,逻辑层,数据层。

  1. UI层:UI层即是ListView控件。
  2. 数据层:要展示到ListView控件上面的数据。
  3. 逻辑层:Adapter的功能是将数据源通过Adapter内部定义的逻辑绑定到ListView控件。

ListView绑定数据相关的组件介绍:

  • ListView控件:是一个列表控件,主要用来承载列表中的每个控件
  • item layout resource:用来定义ListView中每个Item的样式。
  • Adapter:用来将数据源绑定到ListView控件,并可以实现控件的利用。

ListView数据源绑定方式

  • ArrayAdapter
  • SimpleAdapter
  • Custom Adapter

ArrayAdapter

这种绑定方式比较简单直接绑定即可,代码如下:

listViewListAdapter.setAdapter((ListAdapter) arrayAdapter());

private Adapter ArrayAdapter(){

List data = new ArrayList();

data.add("1");

data.add("2");

data.add("3");

data.add("4");

   return new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,data)
}

SimpleAdapter

listViewPersons.setAdapter((ListAdapter)simpleAdapter(getSampleData()));

private Adapter simpleAdapter(List list){

List<HashMap<String,Object>> data=new ArrayList<HashMap<String, Object>>();

    for (Person person:list){
HashMap<String,Object> item=new HashMap<>();
item.put("name",person.getName());
item.put("sex",person.getSex());
item.put("age",person.getAge());
item.put("address",person.getAddress()); data.add(item);
} SimpleAdapter simpleAdapter=new SimpleAdapter(getApplicationContext(),data,R.layout.item,new String[]{"name","sex","age","address"},new int[]{R.id.textViewName,R.id.textViewSex,R.id.textViewAge,R.id.textViewAddress}); return simpleAdapter;
}

Custom Adapter

public class Person {

private String name;

private String sex;

private String age;

private String address;

public Person(String name, String sex, String age, String address) {
this.name = name;
this.sex = sex;
this.age = age;
this.address = address;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}

}

public class PersonAdapter extends BaseAdapter {

private List persons;

private int resource;

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 textViewName=null;
TextView textViewSex=null;
TextView textViewAge=null;
TextView textViewAddress=null; if(convertView==null){
convertView=inflater.inflate(resource,null); textViewName=(TextView)convertView.findViewById(R.id.textViewName);
textViewSex=(TextView)convertView.findViewById(R.id.textViewSex);
textViewAge=(TextView)convertView.findViewById(R.id.textViewAge);
textViewAddress=(TextView)convertView.findViewById(R.id.textViewAddress); ViewCache viewCache=new ViewCache();
viewCache.setTextViewName(textViewName);
viewCache.setTextViewSex(textViewSex);
viewCache.setTextViewAge(textViewAge);
viewCache.setTextViewAddress(textViewAddress); convertView.setTag(viewCache);
}else{
ViewCache viewCache=(ViewCache)convertView.getTag(); textViewName=viewCache.getTextViewName();
textViewSex=viewCache.getTextViewSex();
textViewAge=viewCache.getTextViewAge();
textViewAddress=viewCache.getTextViewAddress();
} Person person=persons.get(position);
textViewName.setText(person.getName());
textViewSex.setText(person.getSex());
textViewAge.setText(person.getAge());
textViewAddress.setText(person.getAddress()); return convertView;
} public class ViewCache{
private TextView textViewName;
private TextView textViewSex;
private TextView textViewAge;
private TextView textViewAddress; public TextView getTextViewName() {
return textViewName;
} public void setTextViewName(TextView textViewName) {
this.textViewName = textViewName;
} public TextView getTextViewSex() {
return textViewSex;
} public void setTextViewSex(TextView textViewSex) {
this.textViewSex = textViewSex;
} public TextView getTextViewAge() {
return textViewAge;
} public void setTextViewAge(TextView textViewAge) {
this.textViewAge = textViewAge;
} public TextView getTextViewAddress() {
return textViewAddress;
} public void setTextViewAddress(TextView textViewAddress) {
this.textViewAddress = textViewAddress;
}
}

}

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listViewPersons.setAdapter((ListAdapter) customAdapter(getSampleData()));
} private Adapter customAdapter(List<Person> list){
PersonAdapter personAdapter=new PersonAdapter(getApplicationContext(),list,R.layout.item); return personAdapter;
}

}

Sample data

private List getSampleData(){

List persons=new ArrayList();

persons.add(new Person("萧峰","男","32","大辽"));

persons.add(new Person("阿朱","女","16","江南"));

persons.add(new Person("段誉","男","22","大理"));

persons.add(new Person("王语嫣","女","18","江南"));

persons.add(new Person("虚竹","男","24","河南"));

persons.add(new Person("梦姑","女","20","西夏"));

    return persons;
}

Android ListView绑定数据的更多相关文章

  1. xamarin.android listview绑定数据及点击事件

    前言 listview是用来显示数据列表的一个控件,今天给大家带来如何使用cursor进行数据绑定以及点击事件. 导读 1.如何创建一个listview 2.如何使用cursor进行绑定数据 3.li ...

  2. AlertDialog中使用ListView绑定数据

    在实际工作过程中,单单使用AlertDialog的单选功能不一定能满足我们的需求,需要绑定数据到 listview 1. 自定义Layout LayoutInflater factory = Layo ...

  3. Xamarin.Android 使用ListView绑定数据

    使用ListView进行数据绑定 第一步:新建一个类,用于存储显示字段. namespace ListViewDataBIndDemo { public class Person { public i ...

  4. android ListView列表显示数据

    item.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  5. android ListView内数据的动态添加与删除

    main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  6. android listview 添加数据

    <span style="white-space:pre"> </span>listView = (ListView) findViewById(R.id. ...

  7. Android之ListView性能优化——一行代码绑定数据——万能适配器

    如下图,加入现在有一个这样的需求图,你会怎么做?作为一个初学者,之前我都是直接用SimpleAdapter结合一个Item的布局来实现的,感觉这样实现起来很方便(基本上一行代码就可以实现),而且也没有 ...

  8. 使用自定的Adapter绑定ListView/GridView数据

    使用自定的Adapter绑定ListView/GridView数据(地址) 对于ListView/Gridview的数据绑定, google提供了一些Adapter的模板, 自己也可以自定义一些个性化 ...

  9. Android ListView异步加载数据

    1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...

随机推荐

  1. iOS开发 - NSScanner的使用方法

    NSScanner这个类,用于在字符串中扫描指定的字符. 能够在创建NSScanner时指定它的string属性.然后scanner会依照要求从头到尾地扫描这个字符串中的每一个字符.扫描动作会使扫描仪 ...

  2. [Java] 实验5參考代码

    实验4月3日晚截止,实验截止后将在此给出完整的參考代码. 1. 怎样使用以下的代码模板: 1.1 在eclipse中创建相应名称的类     1.2 将代码拷贝到类文件中 1.3 在//todo凝视中 ...

  3. linux下配置LAMP开发环境,以及经常使用小细节

    本来安装没什么可说到.可是在linux其中easy会出现各种各样到问题. 我安装以后导致各种问题 比方php无法正常解析,数据库无法关闭,Apache无法开启等等........ 所以搞得我比較郁闷, ...

  4. (十)Net Core项目使用Cookies (八)Net Core项目使用Controller之三-入参

    (十)Net Core项目使用Cookies 一.简介 1.Net Core可以直接使用Cookies,但是调用方式有些区别. 2.Net Core将Request和Response分开实现. 二.基 ...

  5. C语言中结构体变量之间赋值

    近期,我阅读了某新员工小刘写的C语言代码,发现其对结构体变量之间的赋值不是非常熟悉. 对于两个同样类型的结构体变量,他均採用的是逐个成员变量直接赋值的形式.例如以下的代码演示样例: 如上代码所看到的, ...

  6. ≪统计学习精要(The Elements of Statistical Learning)≫课堂笔记(三)

    照例文章第一段跑题,先附上个段子(转载的哦~): I hate CS people. They don't know linear algebra but want to teach projecti ...

  7. C++ - 使用copy函数打印容器(container)元素

    使用copy函数打印容器(container)元素 本文地址: http://blog.csdn.net/caroline_wendy C++能够使用copy函数输出容器(container)中的元素 ...

  8. Android仿微信朋友圈图片查看器

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/40264551 看博文之前,希望大家先打开自己的微信点到朋友圈中去,细致观察是不是发 ...

  9. 2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图

    Ponds                                   Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 1310 ...

  10. HDU 5979 Convex【计算几何】 (2016ACM/ICPC亚洲区大连站)

    Convex Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...