Android中的適配器

數(shù)組適配器(ArrayAdapter)

  • 需要傳的參數(shù)類(lèi)型
  • 實(shí)現(xiàn)效果:


    效果圖
  • 需要的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();//獲取系統(tǒng)資源對(duì)象
        arrCity = resources.getStringArray(R.array.city);//通過(guò)資源對(duì)象獲取資源
        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文件下創(chuàng)建一個(gè)存放數(shù)組類(lèi)型的xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="city">
        <item>北京</item>
        <item>廣州</item>
        <item>成都</item>
    </string-array>
</resources>

簡(jiǎn)單適配器(SimpleAdapter)

  • 參數(shù)類(lèi)型


    參數(shù)類(lèi)型
  • 需要注意的幾點(diǎn)
  • List<? extends Map<String,?>> data :泛型
    HashMap繼承Map
    ?:代表任意類(lèi)型(object)
    定義:
    private List<HashMap<String,Object>> data =new ArrayList<>();
  • 第三個(gè)參數(shù),為L(zhǎng)ist的layout,需要在layout文件夾中添加對(duì)應(yīng)的layout
  • 第四個(gè)參數(shù)列表和第五個(gè)參數(shù)列表應(yīng)一一對(duì)應(yīng)
  • 實(shí)現(xiàn)效果


    效果圖
  • 需要的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<>();
            //鍵值對(duì),Key:為String類(lèi)型,value:為任何類(lèi)型
            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所對(duì)應(yīng)的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所對(duì)應(yīng)的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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,388評(píng)論 0 17
  • 個(gè)人理解 例子:適配器模式是為解決什么問(wèn)題而生的?在網(wǎng)上看到一個(gè)例子,中國(guó)的電源插座都是扁平的三腳或者雙腳插頭,而...
    跑步與開(kāi)車(chē)閱讀 1,253評(píng)論 0 3
  • 所有知識(shí)點(diǎn)已整理成app app下載地址 J2EE 部分: 1.Switch能否用string做參數(shù)? 在 Jav...
    侯蛋蛋_閱讀 2,716評(píng)論 1 4
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,225評(píng)論 0 3
  • 1. 簡(jiǎn)介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存儲(chǔ)過(guò)程以及高級(jí)映射的優(yōu)秀的...
    笨鳥(niǎo)慢飛閱讀 6,282評(píng)論 0 4

友情鏈接更多精彩內(nèi)容