ListViewとArrayAdapter使い方

目次へ

ListViewとAdapter

ListViewは複数のデータを表示するのに使いやすい部品です。
データの一つ一つがひとつの項目からなるデータの場合にも、 データの一つが複数の項目からなり、2次元の表のような形にするする場合にも使えます。
また、データ自体はAdapterクラスが管理し、Viewとの橋渡しを行います。

一番簡単な使い方は、複数のデータを配列あるいはリストの形で作成し、 ArrayAdapterを使ってListViewに表示させるものです。 ここではListViewとArrayAdapterを使う方法を説明します。
項目の表示をカスタマイズする場合には、SimpleAdapterを使います。 この方法はこちらご覧ください。

ListViewとArrayAdapterの使い方

手順

  • レイアウトファイル(例:activity_main.xml)にListViewを作成します
  • 1行分のデータをどのようなレイアウトで作成するかを示すレイアウトファイル(例:row.xml)を作成します
  • ListViewの1番上にタイトルをつけるならレイアウトで作成するかを示す レイアウトファイル(例:row_header.xml)を作成します
  • データの配列を作成します
  • ArrayAdapterのインスタンスを作成し、1行分のレイアウトファイル(row.xml)と、その中のどこに表示するか、 データの配列を指定します。
  • ListViewにArrayAdapterのインスタンスをセットします
  • 必要ならListViewをタップした時のリスナを作成します
  • 必要ならListViewの1番上にタイトルをつけます

activity_main.xmlの例


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="こんにちは一覧"
        app:layout_constraintBottom_toTopOf="@+id/listView1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/title"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"  />
</androidx.constraintlayout.widget.ConstraintLayout>

row.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">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

row_header.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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#999999"
        android:text="row_headerで入れた項目のタイトル" />
</LinearLayout>

MainActivityの例

この中で、ListViewに表示するデータを作成し、ArrayAdapterを作成し、 ListViewにAdapterをセットし、ListViewの項目をクリックしたときのリスナーをセットし、 ListViewの1番上にタイトルをセットしています。

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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{
    ListView list;
    ArrayAdapter<String> adapter;

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

        list = findViewById(R.id.listView1);

        //------------ データ作成
        String[] data = {"グッド アフタヌーン","こんんちは","ニーハオ","ナマステ","ボンジュール"};
        
        //------------ タイトル作りListViewにセットする
        View header = (View)getLayoutInflater().inflate(R.layout.row_header,null);
        list.addHeaderView(header);

        //------------ ArrayAdapterを作りListViewにセットする
        adapter = new ArrayAdapter<String>( 
                this,           //コンテキスト
                R.layout.row,   //1行分のXML
                R.id.text1,     //データを表示する部品(row.xmlの中で指定)
                data            //データ
        );
        list.setAdapter(adapter);
        
        //------------ ListViewの項目をクリックしたときのリスナ 
        list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
                //------------ ヘッダがあるとpositionは1から始まり、rowIdは0から始まる
                //------------ ヘッダがなければpositionもrowIdも0から始まる
                String str = (String)adapter.getItem((int)rowId) + "position="+position+" rowId="+rowId;
                Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
            }
        });
    }
}

コメント

このブログの人気の投稿

ListViewの使い方

AsyncTaskについて

ListViewとSimpleAdapter使い方