ArrayAdapter,SimpleAdapter,自定义Adapter区别代码

package com.example.jump;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.adapter.MyAdapter;
import com.example.entity.CarCompany;

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

public class ArrayAdapterActivity extends AppCompatActivity {
    private ListView lv;
    private List<String> str_list;
    private String[] names=new String[]{"长安", "沃尔沃", "大众", "宾利", "奔驰"};
    private int[] ids=new int[]{R.drawable.changan,R.drawable.vol,R.drawable.das,R.drawable.bl,R.drawable.benz};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_array_adapter);
        lv= (ListView) findViewById(R.id.lv_list);
//        intiArrayList();
//        intiCustomAdapter();
        intiSimpleAdapter();

    }
    //系统ArrayAdapter(线性布局适配器)
    private void intiArrayList(){
        //数据构建完成
        str_list=new ArrayList<String>();
        str_list.add("Android");
        str_list.add("IOS");
        str_list.add("WindowsPhone");
        //Adapter
        ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,str_list);
        //将适配器设置给listview
        lv.setAdapter(adapter);
    }
    //新建list存放carcompany数据
    //自定义Adapter,可以实现图文混排和使用网络图片
    private void intiCustomAdapter(){
        List<CarCompany> list=new ArrayList<CarCompany>();
        for (int i = 0; i < names.length; i++) {
            CarCompany company=new CarCompany();
            company.setImg(ids[i]);
            company.setCompany(names[i]);
            list.add(company);
        }
        MyAdapter myadapter=new MyAdapter(list,this);
        lv.setAdapter(myadapter);
    }
    //系统SimpleAdapter,没有逻辑的图文混排
    private void intiSimpleAdapter(){
        List<HashMap<String,Object>> list=new ArrayList();
        for (int i = 0; i <names.length ; i++) {
            HashMap<String,Object> map=new HashMap<>();
            map.put("name",names[i]);
            map.put("img",ids[i]);
            list.add(map);
        }
        //上下文对象 数据源 资源文件的id
        //String[] from
        //int[]  to
        String[] from=new String[]{"name","img"};
        int[] to =new int[]{R.id.name,R.id.iv_img};
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.activity_item_list,from,to);
        lv.setAdapter(adapter);
    }
}
上一篇:el-upload配合腾讯云cos制作文件上传组件


下一篇:腾讯云COS分片上传