main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.lune.adapter_01.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
Bean类
public class Bean {
private String title;
private String desc;
private String time;
private String phone;
public Bean() {
}
public Bean(String title, String desc, String time, String phone) {
this.title = title;
this.desc = desc;
this.time = time;
this.phone = phone;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
item布局item_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:background="#aae79e"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/mtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="标题1"
android:textColor="#444"
android:textSize="16sp" />
<TextView
android:id="@+id/mdesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="10dp"
android:minLines="1"
android:maxLines="2"
android:text="Android打造万能的ListView和GridView适配器"
android:textColor="#898989"
android:textSize="16sp"/>
<TextView
android:id="@+id/mtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/desc"
android:layout_marginTop="10dp"
android:text="2016-9-16"
android:textColor="#898989"
android:textSize="12sp"/>
<TextView
android:id="@+id/mphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/desc"
android:layout_marginTop="10dp"
android:drawableLeft="@drawable/phone"
android:drawablePadding="5dp"
android:text="10086"
android:padding="3dp"
android:textColor="#fff"
android:background="#66ae5a"
android:textSize="12sp"/>
</RelativeLayout>
自定义Adapter类继承于BaseAdapter
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Bean> mDatas;
public MyAdapter(Context context , List<Bean> datas) {
//context用于获得LayoutInflater,进而加载item布局
mInflater = LayoutInflater.from(context);
mDatas = datas;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
//加载布局
convertView = mInflater.inflate(R.layout.item_listview,parent,false);
holder = new ViewHolder();
//对holder的控件进行查找
holder.mTitle = (TextView)convertView.findViewById(R.id.mtitle);
holder.mDesc = (TextView)convertView.findViewById(R.id.mdesc);
holder.mTime = (TextView)convertView.findViewById(R.id.mtime);
holder.mPhone = (TextView)convertView.findViewById(R.id.mphone);
convertView.setTag(holder);
}else { //convertView进行复用,convertView中已经设置过Tag
holder =(ViewHolder)convertView.getTag();
}
//为holder里面的控件设置值
Bean bean = mDatas.get(position);
holder.mTitle.setText(bean.getTitle());
holder.mDesc.setText(bean.getDesc());
holder.mTime.setText(bean.getTime());
holder.mPhone.setText(bean.getPhone());
return null;
}
private class ViewHolder{
TextView mTitle;
TextView mDesc;
TextView mTime;
TextView mPhone;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<Bean> datas;
MyAdapter madpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initDatas();
initViews();
}
private void initDatas() {
datas = new ArrayList<Bean>();
Bean bean1 = new Bean("标题1" , "Android打造万能的ListView和GridView适配器" ,
"2016-9-16" , "10086");
Bean bean2 = new Bean("标题2" , "Android打造万能的ListView和GridView适配器" ,
"2016-9-16" , "10086");
Bean bean3 = new Bean("标题3" , "Android打造万能的ListView和GridView适配器" ,
"2016-9-16" , "10086");
Bean bean4 = new Bean("标题4" , "Android打造万能的ListView和GridView适配器" ,
"2016-9-16" , "10086");
datas.add(bean1);
datas.add(bean2);
datas.add(bean3);
datas.add(bean4);
madpter = new MyAdapter(this,datas);
}
private void initViews() {
listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(madpter);
}
}