Android中的适配器

数组适配器(ArrayAdapter)

  • 需要传的参数类型
  • 实现效果:


    效果图
  • 需要的java代码:

package com.example.demo2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ListView lv_city;
    private String[] arrCity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Resources resources = getResources();//获取系统资源对象
        arrCity = resources.getStringArray(R.array.city);//通过资源对象获取资源
        lv_city= findViewById(R.id.lv_city);
        //定义适配器
        ArrayAdapter<String> adapter=new ArrayAdapter<>(
                this,
                android.R.layout.simple_list_item_1,
                arrCity
        );
        lv_city.setAdapter(adapter);
        lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, arrCity[i], Toast.LENGTH_LONG).show();
            }
        });
    }


  • 需要的xml代码
<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/lv_city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>
  • 在value文件下创建一个存放数组类型的xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="city">
        <item>北京</item>
        <item>广州</item>
        <item>成都</item>
    </string-array>
</resources>

简单适配器(SimpleAdapter)

  • 参数类型


    参数类型
  • 需要注意的几点
  • List<? extends Map<String,?>> data :泛型
    HashMap继承Map
    ?:代表任意类型(object)
    定义:
    private List<HashMap<String,Object>> data =new ArrayList<>();
  • 第三个参数,为List的layout,需要在layout文件夹中添加对应的layout
  • 第四个参数列表和第五个参数列表应一一对应
  • 实现效果


    效果图
  • 需要的java代码
package com.example.demo2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

public class FirstActivity extends AppCompatActivity {
    private ListView lv_person;
    private List<HashMap<String,Object>> data =new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        for (int i=0;i<5;i++){
            HashMap<String,Object> map=new HashMap<>();
            //键值对,Key:为String类型,value:为任何类型
            map.put("head",R.mipmap.ic_launcher);
            map.put("name","冯"+i);
            map.put("info","哈哈哈哈"+i);
            map.put("time","20:0"+i);

            data.add(map);
        }

        lv_person=findViewById(R.id.lv_person);
        SimpleAdapter adapter=new SimpleAdapter(
                this,
                data,
                R.layout.item,
                new String[]{"head","name","info","time"},
                new int[]{R.id.iv_head,R.id.tv_name,R.id.tv_info,R.id.tv_time}

        );
        lv_person.setAdapter(adapter);
    }
}

  • 需要的java所对应的xml代码
<?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"
    android:orientation="vertical"
    tools:context=".FirstActivity">
    <ListView
        android:id="@+id/lv_person"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

</LinearLayout>
  • 需要的Hashmap所对应的xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <ImageView
        android:id="@+id/iv_head"
        android:src="@mipmap/ic_launcher"
        android:layout_width="80dp"
        android:layout_height="80dp" />
    <TextView
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/iv_head"
        android:layout_marginLeft="20dp"
        android:id="@+id/tv_name"
        android:text="掌声"
        android:textSize="20sp"
        android:textColor="#000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/iv_head"
        android:layout_alignBottom="@id/iv_head"
        android:layout_marginLeft="20dp"
        android:id="@+id/tv_info"
        android:text="哈哈哈哈哈"
        android:textSize="14sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:textSize="14sp"
        android:text="20:00"
        android:id="@+id/tv_time"
        android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,909评论 0 17
  • 个人理解 例子:适配器模式是为解决什么问题而生的?在网上看到一个例子,中国的电源插座都是扁平的三脚或者双脚插头,而...
    跑步与开车阅读 4,935评论 0 3
  • 所有知识点已整理成app app下载地址 J2EE 部分: 1.Switch能否用string做参数? 在 Jav...
    侯蛋蛋_阅读 7,397评论 1 4
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 7,938评论 0 3
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 11,216评论 0 4

友情链接更多精彩内容