public abstract class CommonAdapter extends BaseAdapter {
protected Context mContext;
protected ArrayList mArrayList;
protected final int mItemLayoutId;
protected LayoutInflater mInflater;
public CommonAdapter(Context mContext, ArrayList<T> mArrayList, int mItemLayoutId) {
this.mContext = mContext;
this.mInflater = LayoutInflater.from(mContext);
this.mArrayList = mArrayList;
this.mItemLayoutId = mItemLayoutId;
}
@Override
public int getCount() {
return mArrayList.size();
}
@Override
public T getItem(int position) {
return mArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder = getViewHolder(position, convertView, parent);
convert(viewHolder, getItem(position));
return viewHolder.getConvertView();
}
public abstract void convert(ViewHolder helper, T item) ;
private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent){
return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);
}
}
public ViewHolder setText(int viewId, String text)
{
TextView view = getView(viewId);
view.setText(text);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param drawableId
* @return
*/
public ViewHolder setImageResource(int viewId, int drawableId)
{
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this;
}
/**
* 为ImageView设置图片
*
* @param viewId
* @param
* @return
*/
public ViewHolder setImageBitmap(int viewId, Bitmap bm)
{
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
}
public int getPosition()
{
return mPosition;
}
}
用的时间就比较简单了
public class MainActivity extends AppCompatActivity {
private ListView mListview;
private ArrayList list = new ArrayList();
private ListBean listBean;
CommonAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListview = (ListView) findViewById(R.id.main_list);
adapter = new CommonAdapter<ListBean>(getApplicationContext(), list, R.layout.item_listview) {
@Override
public void convert(ViewHolder helper, ListBean item) {
helper.setText(R.id.tv_title, item.getTitle());
helper.setText(R.id.tv_describe, item.getContent());
helper.setText(R.id.tv_time, item.getTime());
helper.setText(R.id.tv_phone, item.getPhoneNum());
}
};
mListview.setAdapter(adapter);
add();
}
//写一个方法,专门往listview 添加数据
public void add(){
for (int i = 0; i < 20; i++) {
listBean=new ListBean("1","2","3","4");
list.add(listBean);
}
adapter.notifyDataSetChanged();
}
}
public class ListBean {
private String title;
private String content;
private String time;
private String phoneNum;
public ListBean(String title, String content, String time, String phoneNum) {
this.title = title;
this.content = content;
this.time = time;
this.phoneNum = phoneNum;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}
这是样式的布局 item_listview