ArrayAdapter、SimpleAdapter、BaseAdapter的基本使用

ArrayAdapter:基本使用:


MainActivity布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mrzhao.adapterdemo.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

条目布局:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:textColor="@color/colorAccent"
    android:textSize="20sp">

</TextView>
MainActivity:

/**
 * ArrayAdapter 数组适配器  只能显示文本内容的适配器
 */

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private List<String> list = new ArrayList<>();
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        //初始化数据源
        iniData();
        /**
         * 参数1 上下文对象
         * 参数2 条目布局的id 可以使用系统提供的 id
         * 参数3 数据源
         */
        adapter = new ArrayAdapter<>(this, R.layout.item_layout, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, position + "----" + list.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    //初始化一个数据源
    private void iniData() {
        list.add("张磊");
        list.add("张磊别睡了");
        list.add("张磊醒醒吧");
        list.add("张磊太阳还没下山呢");
        list.add("张磊生活多么的美好啊");
        list.add("张磊睡觉多浪费啊");
        list.add("张磊来吧");
        list.add("张磊跳楼吧");
        list.add("张磊");
        list.add("张磊别睡了");
        list.add("张磊醒醒吧");
        list.add("张磊太阳还没下山呢");
        list.add("张磊生活多么的美好啊");
        list.add("张磊睡觉多浪费啊");
        list.add("张磊来吧");
        list.add("张磊跳楼吧");
        list.add("张磊");
        list.add("张磊别睡了");
        list.add("张磊醒醒吧");
        list.add("张磊太阳还没下山呢");
        list.add("张磊生活多么的美好啊");
        list.add("张磊睡觉多浪费啊");
        list.add("张磊来吧");
        list.add("张磊跳楼吧");
    }
}


SimpleAdapter基本使用:

MainActivity布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mrzhao.simpleradapterdemo.MainActivity">

</ListView>

条目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/isSingle_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="singleDog" />

    <ImageView
        android:id="@+id/headPic_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />
</LinearLayout>

MainActivity:

/**
 * SimpleAdapter  简单的适配器
 * SimpleCursorAdapter 这个是配合数据库使用的
 */
public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private List<Map<String, Object>> list = new ArrayList<>();
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);

        //定义数据源
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name","张磊");
        map1.put("isSingle",true);
        map1.put("headPic",R.mipmap.ic_launcher);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name","王世兴");
        map2.put("isSingle",false);
        map2.put("headPic",R.mipmap.ic_launcher);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("name","小崔");
        map3.put("isSingle",true);
        map3.put("headPic",R.mipmap.ic_launcher);

        Map<String, Object> map4 = new HashMap<>();
        map4.put("name","柴伟");
        map4.put("isSingle",true);
        map4.put("headPic",R.mipmap.ic_launcher);

        //添加到集合中
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);

        /**
         * 参数1 上下文对象
         * 参数2 数据源集合 必须是List<Map>
         * 参数3 条目布局Id
         * 参数4 String数组内容是Map中所有key的集合
         *  参数5 int数组 显示 mapkey对应的value数据的视图Id的集合
         *
         *
         */
        simpleAdapter = new SimpleAdapter(this,list, R.layout.item_layout,new String[]{"name","isSingle","headPic"},new int[]{R.id.name_tv,R.id.isSingle_cb,R.id.headPic_iv});
        listView.setAdapter(simpleAdapter);

    }
}


BaseAdapter基本用法(无优化):

MainActivity布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mrzhao.baseadapterdemo.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>


item条目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:src="@mipmap/ic_launcher_round"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/age_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/info_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:textSize="20sp" />
</LinearLayout>

Persen类:

public class Persen {
    private String name;
    private String age;
    private String info;

    public Persen(String name, String age, String info) {
        this.name = name;
        this.age = age;
        this.info = info;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Persen{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", info='" + info + '\'' +
                '}';
    }
}

BaseAdapter:

public class MyAdapter extends BaseAdapter {

    //定义一个数据源
    private List<Persen> list;
    //定义一个上下文对象
    private Context  context;

    //构造方法
    public MyAdapter(List<Persen> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @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;
    }

    /**
     *返回每个条目的视图     position =  多少的时候就是返回第几个条目的视图
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //使用布局加载器将xml布局文件转换成为 JavaView视图对象
        //初始化一个布局加载器
        LayoutInflater inflater = LayoutInflater.from(context);
        /**
         * 参数1 是布局的id
         * 参数2 表示的是这个布局的父布局是谁
         * 参数3 写死  false!!!! 为了给条目设置父布局的LayoutParams
         */
        View  view =inflater.inflate(R.layout.item_layout,parent,false);
        TextView nameTv = (TextView) view.findViewById(R.id.name_tv);
        TextView ageTv = (TextView) view.findViewById(R.id.age_tv);
        TextView infoTv = (TextView) view.findViewById(R.id.info_tv);

        Persen persen = list.get(position);
        nameTv.setText(persen.getName());
        ageTv.setText(persen.getAge());
        infoTv.setText(persen.getInfo());
        return view;
    }
}

MainActivity类:

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private List<Persen> list = new ArrayList<>();
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);

        //初始化数据源
        for (int i = 0; i < 100; i++) {
            list.add(new Persen("名字"+i,i+"","呵呵"));
        }

        myAdapter = new MyAdapter(list,this);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, list.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值