Android基礎-適配器

適配器的作用:適配器是視圖與數據之間的橋樑,通過適配器我們可以將我們的數據填充在相應的控件之中。我們常用的適配器有三種,ArrayAdapter,SimpleAdapter,SimpleCursorAdapter 這三個,他們都是繼承於BaseAdapter。

1.ArrayAdapter
ArrayAdapter是Android的列表適配器
首先創建一個Activity,其xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.example.dfcn.myapplication.TextActivity">
    <ListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</android.support.constraint.ConstraintLayout>

activity代碼

package com.example.dfcn.myapplication;


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



public class TextActivity extends AppCompatActivity {
    private ListView listView;
    private String[] city={
            "北京","上海","深圳","四川"
    };//創建數據
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        listView=findViewById(R.id.lv_list);

        ArrayAdapter arrayAdapter=new ArrayAdapter(TextActivity.this,R.layout.support_simple_spinner_dropdown_item,city);//這裏面的參數(上下文,佈局文件中item的id,數據)
        listView.setAdapter(arrayAdapter);//將listView綁定在適配器中

    }
}

ArrayAdpter這種適配器只是一種簡單適配器,只能顯示單一的TextView
R.layout.support_simple_spinner_dropdown_item 這個是Android自帶的佈局item
這裏寫圖片描述

2.SimpleAdapter
SimpleAdapter繼承BaseAdpter。實現了BaseAdpter的4中抽象方法,分別是getCount(),getItem(),getItemId(),getView方法。

這裏寫圖片描述

首先創建具體佈局xml文件 item.xml

<?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">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/iv_pic"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_name"
            android:textSize="40sp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="60dp" />
    </LinearLayout>

</LinearLayout>

讓後創建一個activity

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

</ListView>
package com.example.dfcn.myapplication;


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

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


public class TextActivity extends AppCompatActivity {
    private ListView listView;
    private String[] name={
            "張三","李四","王五","王二麻子"
    };
    private int[] pic={
            R.mipmap.ic_launcher
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        listView=findViewById(R.id.lv_list);

        List<Map<String,Object>>  list=new ArrayList<Map<String, Object>>();
        for (int i=0;i<name.length;i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name",name[i]);
            map.put("pic",pic[0]);
            list.add(map);
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.item,new String[]{"name","pic"},new int[]{R.id.tv_name,R.id.iv_pic});
        listView.setAdapter(simpleAdapter);
        }

    }

SimpleAdpter對象內有5個參數
Context context:上下文(當前的Activity)
data 一個Map型列表。列表中的每個條目對應於列表中的一行。Map中包含每一行的數據,並且包括所有的條目(其被詳細說明在數據源處)
( 把data理解爲要裝載的數據即可)
resourse 一個View佈局的資源標記,其定義了佈局中的列表項,佈局文件至少包含那些需要展示的視圖項
(就是想要展示的佈局樣式)

form 列名的列表,其在Map中對應着每一項數據item
to 根據‘form’參數可以得到的數值,對應的值就是根據其‘from’參數的列表的某個值所得到的。

3.BaseAdpter
BaseAdpter是一個自定義的適配器,它實際上是一個抽象類。

首先 創建ListView程序對應的佈局文件(activity_main.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"
    tools:context="com.example.dfcn.myapplication.TextActivity">
<ListView
    android:id="@+id/lv_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</ListView>
</LinearLayout>

讓後在單獨的創建一個xml佈局

<?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">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/iv_pic"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_name"
            android:textSize="40sp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="60dp" />
    </LinearLayout>

</LinearLayout>

編寫界面交互代碼(MainActivity)

package com.example.dfcn.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;




public class TextActivity extends AppCompatActivity {
    private ListView listView;
    private String[] name = {
            "張三", "李四", "王五", "王二麻子"
    };
    private int[] pic = {
            R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher
    };

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

        //創建一個Adpter的實例
        MyBaseAdpter myBaseAdpter=new MyBaseAdpter();
        //綁定適配器
        listView.setAdapter(myBaseAdpter);


    }
    class MyBaseAdpter extends BaseAdapter{
        @Override
        public int getCount() {
            //返回ListView Item條目的總數
            return name.length;
        }

        @Override
        public Object getItem(int i) {
            //得到Item代表的對象
            return name[i];
        }

        @Override
        public long getItemId(int i) {
            //得到 ListView的id
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //將item.xml文件找出來 並轉化成View對象
            View v=View.inflate(TextActivity.this,R.layout.item,null);
            //找到item.xml中的控件id
            ImageView imageView=findViewById(R.id.iv_pic);
            imageView.setBackgroundResource(pic[i]);
            TextView textView=findViewById(R.id.tv_name);
            textView.setText(name[i]);
            return v;
        }
    }
}

這裏寫圖片描述
也可以實現 上面的效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章