传统的Adapter

main.xml




    

    


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




    

    

    

    

自定义Adapter类继承于BaseAdapter

public class MyAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List mDatas;

    public MyAdapter(Context context , List 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 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 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);
    }
}




你可能感兴趣的:(android,Adapter,Adapter)