Android笔记-自定义适配器

Android之自定义适配器

当我们需要把数据集按某种规则系列地显示在android屏幕上时,这时候就需要用到listview以及它的适配器了。Adapter是所有适配器的根接口,ListAdapter是Adapter的子接口,BaseAdapter是实现ListAdapter接口的抽象类,那么我们要制定自己的适配器时就需要继承BaseAdapter了,那么它里面有些方法就需要我们去实现了。

int getCount();    返回条目的总数

Object getItem(int position);

BaseAdapter long getItemId(int i);

View getView(int position, View convertView, ViewGroup parent);   返回当前页面的listView

比较重要的就是getView()方法。

那么我们要把数据显示在适配器上就是在getView()方法里面实现的。自定义适配器有一个好处就是可以对item里的任意组件进行监听。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 自定义视图
ListItemView liv = null;
if (convertView == null) {
liv = new ListItemView();
// 获取list_item布局文件的视图
convertView = listContainer.inflate(R.layout.list_item, null);
liv.time = (TextView) convertView.findViewById(R.id.time);
liv.iv = (ImageView) convertView.findViewById(R.id.head);
liv.user = (TextView) convertView.findViewById(R.id.user);
liv.place = (TextView) convertView.findViewById(R.id.place);
liv.book = (TextView) convertView.findViewById(R.id.book);
liv.chat = (Button) convertView.findViewById(R.id.chat);
liv.save = (Button) convertView.findViewById(R.id.save);
convertView.setTag(liv);
} else {
liv = (ListItemView) convertView.getTag();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
sdf.format(new Date());
liv.time.setText((sdf.format(new Date())));
liv.iv.setBackgroundResource((Integer) listItems.get(position).get("image"));
liv.user.setText((String) listItems.get(position).get("user"));
liv.user.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((TextView) v).getText(), 0).show();
}
});
liv.place.setText((String) listItems.get(position).get("place"));
liv.place.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((TextView) v).getText(), 0).show();
}
});
liv.book.setText((String) listItems.get(position).get("book"));
liv.book.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((TextView) v).getText(), 0).show();
}
});
liv.chat.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((Button) v).getText(), 0).show();
}
});
liv.save.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((Button) v).getText(), 0).show();
}
});
return convertView;
}


在应用第一次显示的时候,需要判断conVertView是否null,如果为null就需要进行加载item的布局文件了,这样做可以极大提高性能

,因为之后就不需要重新加载了而是从缓存中取出来即可,然后后面的工作就是把数据显示在item里的组件上。


实现的效果图:

    Android笔记-自定义适配器_第1张图片

Android笔记-自定义适配器_第2张图片


列表项布局文件list_item.xml:





    


    


        


        


            


                


                
            


            


                


                
            


            
        


        


            


主界面布局文件activity_main.xml:




    


    
    




自定义适配器MyAdapter.java:

package com.example.mylistview;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MyAdapter extends BaseAdapter {
private Context context;
private List> listItems;
private LayoutInflater listContainer;


public final class ListItemView {
public TextView time;
public ImageView iv;
public TextView user;
public TextView place;
public TextView book;
public Button chat;
public Button save;
}


public MyAdapter(Context context, List> listItems) {
this.context = context;
this.listItems = listItems;
this.listContainer = LayoutInflater.from(context);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return listItems.size();
}


@Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return null;
}


@Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 自定义视图
ListItemView liv = null;
if (convertView == null) {
liv = new ListItemView();
// 获取list_item布局文件的视图
convertView = listContainer.inflate(R.layout.list_item, null);
liv.time = (TextView) convertView.findViewById(R.id.time);
liv.iv = (ImageView) convertView.findViewById(R.id.head);
liv.user = (TextView) convertView.findViewById(R.id.user);
liv.place = (TextView) convertView.findViewById(R.id.place);
liv.book = (TextView) convertView.findViewById(R.id.book);
liv.chat = (Button) convertView.findViewById(R.id.chat);
liv.save = (Button) convertView.findViewById(R.id.save);
convertView.setTag(liv);
} else {
liv = (ListItemView) convertView.getTag();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
sdf.format(new Date());
liv.time.setText((sdf.format(new Date())));
liv.iv.setBackgroundResource((Integer) listItems.get(position).get("image"));
liv.user.setText((String) listItems.get(position).get("user"));
liv.user.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((TextView) v).getText(), 0).show();
}
});
liv.place.setText((String) listItems.get(position).get("place"));
liv.place.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((TextView) v).getText(), 0).show();
}
});
liv.book.setText((String) listItems.get(position).get("book"));
liv.book.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((TextView) v).getText(), 0).show();
}
});
liv.chat.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((Button) v).getText(), 0).show();
}
});
liv.save.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(context, ((Button) v).getText(), 0).show();
}
});
return convertView;
}
}


MainActivity.java:

package com.example.mylistview;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;


public class MainActivity extends Activity {
private ListView listview;
private MyAdapter adapter;
private List> listItems;
private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6,
R.drawable.p7, R.drawable.p8, R.drawable.p9, R.drawable.p10 };
String[] users = { "邹文雄", "吕达", "贾国庆", "曹开阳", "方晶晶", "冯绍雄", "馒头", "程红星", "邱志敏", "方晓娜" };
String[] places = { "德国", "阿斯达", "撒打发", "梵蒂冈", "日本", "美国", "俄罗斯", "弗兰克", "英国", "韩国" };
String[] books = { "安徒生童话", "格林童话", "一千零一夜", "金银岛", "吹牛大王奇游记", "童年", "汤姆·索亚历险记", "钢铁是怎样炼成的", "遥远的救世主", "鲁滨逊漂流记",
"高等数学" };


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.list);
listItems = getItems();
adapter = new MyAdapter(this, listItems);
listview.setAdapter(adapter);
}


private List> getItems() {
listItems = new ArrayList>();
for (int i = 0; i < images.length; i++) {
Map map = new HashMap();
map.put("image", images[i]);
map.put("user", users[i]);
map.put("place", places[i]);
map.put("book", books[i]);
listItems.add(map);
}
return listItems;
}
}


点击下载源码

你可能感兴趣的:(Android)