自定义ListView使用的Adapter
2013-06-07 16:33
453 查看
自定义ListView所用的适配器Adapter.
Adapter是用于将数据集(字符串,图片等)和视图(Textview, Image等)进行映射。
ListView的实现需要两个要素:1.数据集,2.适配器Adapter。
举一个实例说明ListView的Adapter的自定义实现。
1. 数据集
使用的是List<Map<String,String>> list
数据集元素的添加方式如下:
Map<String,String>map = newHashMap<String,String>();
map.put("name","Tom");
map.put("age",
"23");
list.add(map);
这样就在数据集中添加了一个元素了。
2. 创建适配器
public
class SelfDefineAdapter extends BaseAdapter{……}
adapter = new SelfDefineAdapter(this,list);
3. 给ListView设置适配器
listView.setAdapter(adapter);
贴上代码:
MainActivity.java
自定义的Adapter:
main.xml
list_row.xml
运行的结果:
Adapter是用于将数据集(字符串,图片等)和视图(Textview, Image等)进行映射。
ListView的实现需要两个要素:1.数据集,2.适配器Adapter。
举一个实例说明ListView的Adapter的自定义实现。
1. 数据集
使用的是List<Map<String,String>> list
数据集元素的添加方式如下:
Map<String,String>map = newHashMap<String,String>();
map.put("name","Tom");
map.put("age",
"23");
list.add(map);
这样就在数据集中添加了一个元素了。
2. 创建适配器
public
class SelfDefineAdapter extends BaseAdapter{……}
adapter = new SelfDefineAdapter(this,list);
3. 给ListView设置适配器
listView.setAdapter(adapter);
贴上代码:
MainActivity.java
package com.nuist.selfdefineadapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ListView; public class MainActivity extends Activity { private ListView listView; private List<Map<String,String>> list; /** * 数组names和ages是用于填充数据集list的。 */ private String[] names = {"Tom","Jerry","Jack"}; private int[] ages = {32,4,56}; private SelfDefineAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.list_people); list = getList(); //数据集,一个List adapter = new SelfDefineAdapter(this,list); //创建适配器,传递了数据集list过去,用于映射到控件 listView.setAdapter(adapter); //为控件ListView设置适配器 } /** * getList()就是讲数组names和ages的内容放到了List中 * @return */ private List<Map<String,String>> getList(){ List<Map<String,String>> listItem = new ArrayList<Map<String,String>>(); for(int i=0;i<ages.length;i++){ Map<String,String> map = new HashMap<String,String>(); map.put("name", names[i]); map.put("age", ages[i]+""); listItem.add(map); } return listItem; } }
自定义的Adapter:
package com.nuist.selfdefineadapter; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class SelfDefineAdapter extends BaseAdapter { private List<Map<String,String>> list; //数据集 private Context context; private LayoutInflater listInflater; //用于视图填充器 public final class ListRowView{ //对应list_raw.xml中的两个TextView控件 TextView name; TextView age; } public SelfDefineAdapter(Context context, List<Map<String,String>> list){ this.context = context; listInflater = LayoutInflater.from(context); this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ListRowView listRowView = null; if(convertView == null){ listRowView = new ListRowView(); convertView = listInflater.inflate(R.layout.list_raw, null); listRowView.name = (TextView)convertView.findViewById(R.id.name); listRowView.age = (TextView)convertView.findViewById(R.id.age); convertView.setTag(listRowView); }else{ listRowView = (ListRowView)convertView.getTag(); } /** * 将数据集中的数据和控件对应起来 */ listRowView.name.setText((String)list.get(position).get("name")); listRowView.age.setText((String)list.get(position).get("age")); return convertView; } }
main.xml
<LinearLayout 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:orientation="vertical"> <ListView android:id="@+id/list_people" android:layout_height="wrap_content" android:layout_width="match_parent" ></ListView> </LinearLayout>
list_row.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="vertical" > <TextView android:id="@+id/name" android:layout_height="wrap_content" android:layout_width="match_parent"/> <TextView android:id="@+id/age" android:layout_height="wrap_content" android:layout_width="match_parent"/> </LinearLayout>
运行的结果:
相关文章推荐
- Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- android中ExpandableListView 使用自定义Adapter的方法
- ListFragment 使用ListView and 自定义Adapter(转载)
- ListView中使用自定义Adapter及时更新数据
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- ListFragment 使用ListView and 自定义Adapter
- ListView中使用自定义Adapter及时更xin
- ListView使用自定义Adapter
- ListView自定义Adapter中getView(int position, View convertView, ViewGroup parent) 使用
- 关于自定义Adapter实现ListView的使用
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(二)
- 关于ListView中使用自定义Adapter及时更新数据
- 27、ListView使用自定义的Adapter
- 使用自定义的BaseAdapter实现LIstView的展示
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据
- 使用自定义的BaseAdapter实现LIstView的展示(转)
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- 使用自定义的BaseAdapter实现LIstView的展示