1. 程式人生 > >Android 簡單介面卡

Android 簡單介面卡

  簡單介面卡 SimpleAdapter應用, 也叫“並不簡單的簡單介面卡”^_^

package com.basic.android08;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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 MainActivity extends AppCompatActivity {
	//獲取測試資料來源
    private int[] images={R.drawable.bird,R.drawable.cat,R.drawable.chicken,R.drawable.cow,R.drawable.dog};
    private String[] titles={"bird","cat","chicken","cow","dog"};
  	//定義ListView 控制元件
    private ListView lv_main_list;
    private SimpleAdapter dapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv_main_list = findViewById(R.id.lv_main_list);
        List<Map<String,Object>> imageList = new ArrayList<>();
        Map<String,Object> map = null;
        for (int i = 0; i < images.length; i++) {
            map = new HashMap<>();
            map.put("title",titles[i]);
            map.put("image",images[i]);
            imageList.add(map);
        }
        // 建立介面卡
         dapter = new SimpleAdapter(this,imageList,
                R.layout.item_listview,new String[]{"title","image"},new int[]{R.id.tv_item_listview_title,R.id.iv_item_listview_icon});
         // 繫結介面卡
        lv_main_list.setAdapter(simpleAdapter2);

    }
}```
//用Android自帶的檔案產生效果
<?xml version="1.0" encoding="UTF-8"?>

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

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

</LinearLayout>

自定義背景:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView android:layout_height="66dp" android:layout_width="66dp" android:id="@+id/iv_item_listview_icon"/>

<TextView android:layout_height="66dp" android:layout_width="0dp" android:id="@+id/tv_item_listview_title" android:textSize="25sp" android:layout_weight="1"/>

</LinearLayout>