自定义适配器:
private class myBaseAdapter extends BaseAdapter { private ArrayList<Contect> list; public myBaseAdapter() { } public myBaseAdapter(ArrayList<Contect> list) { this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Contect contect= (Contect) getItem(position);//传进来的list的某一项 if(convertView==null){ LayoutInflater inflater=LayoutInflater.from(getContext()); convertView=inflater.inflate(R.layout.contect_item,parent,false); } TextView letter_Contect=convertView.findViewById(R.id.letter_Contect); ImageView contect_Head=convertView.findViewById(R.id.contect_Head); TextView contect_Name=convertView.findViewById(R.id.contect_Name); letter_Contect.setText(contect.getLetter()); contect_Head.setImageResource(contect.getHead()); contect_Name.setText(contect.getName()); return convertView; } }
重点是getView()方法:
if(convertView==null){ LayoutInflater inflater=LayoutInflater.from(getContext()); convertView=inflater.inflate(R.layout.contect_item,parent,false); }
BaseAdapter:https://blog.csdn.net/yelbosh/article/details/7831812