ArrayAdapter
1.从文档中介绍来看ArrayAdapter原本就是用来帮助你显示文本的,如果你想要显示其它视图比如:ImageView。就需要自己重写getView()方法
2.如果是这样,我觉得还不如使用SimpleAdapter或是BaseAdapter来得方便
3.构造方法中有四个参数
(1)context --- 包含有关应用环境的全局信息,允许我们访问系统资源和服务,以及我们自己定义的特定资源
由于适配器需要了解每个列表项的布局,因此需要引用我们自己定义的xml布局
(2)id of list item layout --- 列表项的布局
(3)id of text view --- 文本视图的id
(4)list of data --- 数组列表(数据)
List<String> array = new ArrayList<String>();
for(int i=0; i<120; i++) {
array.add("Tom");
}
final Context context = this;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, R.id.text, array){
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listitem, null);
}
TextView textView = (TextView)convertView.findViewById(R.id.text);
ImageView imageView = (ImageView)convertView.findViewById(R.id.image);
textView.setText("Tom");
imageView.setImageResource(R.mipmap.ic_launcher);
return convertView;
}
};
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
SimpleAdapter
1.数据源类型List<? extends Map<String, ?>>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
ListView listView = (ListView)findViewById(R.id.listView);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, getData(), R.layout.listitem,
new String[]{"image", "text"}, new int[]{R.id.image, R.id.text});
listView.setAdapter(simpleAdapter);
}
private List<Map<String, Object>> getData(){
List<Map<String, Object>> list = new ArrayList<>();
for(int i=0; i<120; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", R.mipmap.ic_launcher);
map.put("text", "Tom");
list.add(map);
}
return list;
}
BaseAdapter
1.该适配就比较灵活,因为你可以重写里面的方法来实现自己的适配器
2.需要重写的方法有:
(1)public Object getItem(int position)
(2)public int getCount()
(3)public long getItemId(int position)
(4)public View getView(int position, View convertView, ViewGroup)
在这里文档里建议应该检查convertView是否为空,若为空就应该从新创建一个新的视图,若不为空,直接复用即可
3.你可以通过构造方法将必要的参数传进来
public class TestBaseAdapter extends BaseAdapter {
private List<String> list;
private Context context;
public TestBaseAdapter(List<String> list, Context context){
this.list = list;
this.context = context;
}
//在数据集中有多少个数据项
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
//返回指定位置数据项的id
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//如果为null就创建一个新的视图
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listitem, null);
<span style="font-size:14px;"></span><pre name="code" class="java"> TextView textView = (TextView)convertView.findViewById(R.id.text);
ImageView imageView = (ImageView)convertView.findViewById(R.id.image);
textView.setText("Tom");
imageView.setImageResource(R.mipmap.ic_launcher);
} return convertView; }
}
4.网上看见的一些人的整理
(1)第一种没有任何处理BaseAdapter --- 因为没有复用机制,所以会浪费资源
(2)第二种使用了convertView进行复用 --- 就是我上面的写的
(3)第三种就是通过一个静态类保存视图 --- 将视图保存在缓存中,这样就不用一直findViewById(),提高效率
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = null;
ImageView imageView = null;
//如果为null就创建一个新的视图
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.listitem, null);
textView = (TextView)convertView.findViewById(R.id.text);
imageView = (ImageView)convertView.findViewById(R.id.image);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = textView;
viewHolder.image = imageView;
convertView.setTag(viewHolder);
}else{
ViewHolder viewHolder = (ViewHolder)convertView.getTag();
textView = viewHolder.text;
imageView = viewHolder.image;
}
textView.setText("Tom");
imageView.setImageResource(R.mipmap.ic_launcher);
return convertView;
}
private final class ViewHolder
{
public ImageView image;
public TextView text;
}
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>