當遇到大量且有規律的資料就可以用ListView清單顯示,例如:商品訊息,聯絡人...
ListView是可以上下滑動的清單列表,也可以做出各式各樣的清單
ListView是繼承AdapterView的類別,
但ListView僅是作為列表,用於裝載顯示數據(item),
然而item中的具體資料是由adapter來設定的。
這篇做一個利用ListView來顯示列表
首先 先在activity_main.xml新增ListView
<?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/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
接著在res/drawable下新增一個xml檔來設計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:padding="16dp"
    android:orientation="horizontal">
    
    <ImageView
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/ic_launcher_background"
        android:id="@+id/image"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/otitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Title"
            android:textColor="#000"
            android:textStyle="bold"
            android:layout_margin="5dp"
            android:textSize="20dp"/>
        <TextView
            android:id="@+id/ttitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="sec Title"
            android:textColor="#a9a9a9"
            android:textStyle="bold"
            android:layout_margin="5dp"
            android:textSize="15dp"/>
    </LinearLayout>
</LinearLayout>
畫面設定好之後,將所需的圖片放入res/drawable裡
接著到MainActivity.java裡面設定
package com.example.ittttt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    ListView listView;
    String Title[] = {"可麗露","馬卡龍","瑪德蓮","舒芙蕾","泡芙"};
    String sec[] = {"是一種小型的法式甜點,表層則是硬脆又厚實的褐色焦糖外殼,內部是半融化狀的蛋糕糊,散發著酒香和香草味。",
            "是一種用色彩繽紛絢麗的法國甜品,外殼堅硬但易碎,內陷黏稠扎實",
            "是一種傳統的貝殼形狀的小蛋糕,來自於法國東北部洛林大區的兩個市鎮科梅爾西和利韋爾丹。",
            "是一種源自法國的甜品,經烘焙後質感輕而蓬鬆。",
            "是一種源自法國的球形糕點,蓬鬆張孔的麵皮中包裹鮮奶油、巧克力乃至冰淇淋。"};
    //將圖片檔放入image陣列裡
    int image[] = {R.drawable.coco,R.drawable.mar,R.drawable.der,R.drawable.su,R.drawable.pabu};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listview);
        //新增一個Adapter
        MyAdapter myAdapter = new MyAdapter(this,Title,sec,image);
        listView.setAdapter(myAdapter);
    }
    class MyAdapter extends ArrayAdapter<String>{
        Context context;
        String rTitle[];
        String rsec[];
        int rimage[];
        MyAdapter (Context context,String title[],String sec[],int image[]){
            super(context,R.layout.row,R.id.otitle,title);
            this.context = context;
            this.rTitle = title;
            this.rsec = sec;
            this.rimage = image;
        }
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = layoutInflater.inflate(R.layout.row,parent,false);
            ImageView images = row.findViewById(R.id.qimage);
            TextView textView = row.findViewById(R.id.otitle);
            TextView textView1 = row.findViewById(R.id.ttitle);
            images.setImageResource(rimage[position]);
            textView.setText(rTitle[position]);
            textView1.setText(rsec[position]);
            return row;
        }
    }
}
