[Androidコンテナコンポーネント-AdapterView]

I.概要:

      Androidアプリケーション開発では、AdapterViewは一般的で非常に重要なコンポーネントです。情報をリストの形式で表示する一般的なコンポーネントは、Listviewと呼ばれるAdapterViewのサブクラスです。グリッドで画像のサムネイルを閲覧することが多いコンポーネントは、GridViewと呼ばれるAdapterViewのサブクラスでもあります。次の形式で表示できます。ドロップダウンリスト。オプションコンポーネントは、Spinnerと呼ばれるAdapterViewのサブクラスでもあります。など、これらはすべてAdapterViewのサブクラスです。

2.AdapterViewのプログラミングモードを紹介します。

     AndroidのListViewコンポーネントを使用して、Androidシステムにインストールされているすべてのプログラム情報をリストに表示します。ListViewは、その名前が示すように、リストの形式でユーザーに情報を表示することです。電話設定のリストと同様に、すべてのアプリケーションのアイコン、アプリケーション名(下の図と同様) 、およびエントリActivityのクラス名が含まれています。表示されたコンテンツが物理画面の利用可能な領域を超えると、前号で説明したScrollView効果と同様に、スクロールすることもできます。

       下の図に示すように、大きな黄色のボックスで囲まれた部分はListViewであり、小さな濃い青色のボックスで囲まれた部分はリスト内のリストアイテムです。したがって、ListViewを使用してプログラムに情報を表示するには、いくつかの作業を行う必要があります。

(1)インターフェイスレイアウトにListViewコンポーネントを含める

(2)リストに表示されているリスト項目をレイアウトする

(3)表示する必要のあるデータをListViewコンポーネントに提供するために、Adapterインターフェースを実装するクラスを設計します。

アダプタ:

     上記のリストコンポーネント( ListView)、グリッドコンポーネント(GridView)、およびドロップダウンリストコンポーネント(Spinner )は、すべてAdapterのサブクラスです。これらのコンポーネントは、データの表示のみを担当し、表示するデータを呼び出す必要があります。これは、アダプターのインターフェースによって管理されます。ListViewを使用してデータを表示する例として、AdapterViewとAdapterインターフェースの関係は次のとおりです。

 

アダプタの一般的な方法とその意味:

メソッド名 意味
int getCount() 表示するデータセット内のデータの総数を返します
オブジェクトgetItem(int位置) データセット内の指定された位置にあるデータオブジェクトを返します
long getItemId(int position) データセット内の指定された場所にあるデータのIDを返します

getView(int position、

表示convertView、

ViewGroupの親)

指定された場所のデータをAdapterViewに表示できるコンポーネントにビルドし、AdapterViewに戻って表示します

ListViewは以下を使用します:

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:stretchColumns="2"
    >

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

[新規]→[レイアウト解決ファイル]を選択して作成します

 item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:stretchColumns="2"
    >

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

</RelativeLayout>

 myAdapater.java

package com.example.demo03_22;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class myAdapater extends BaseAdapter {
    private String[] books={"Java程序设计","Android应用开发","oracle数据库管理指南","JavaWeb程序设计","软件工程之系统工程师之路"};
    LayoutInflater inflater;
    int id_item;
    public myAdapater(Context context,int id_item){
        this.id_item=id_item;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return books.length;
    }

    @Override
    public Object getItem(int i) {
        return books[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView tv;
        tv=(TextView) inflater.inflate(id_item,viewGroup,false);
        tv.setText(books[i]);
        return tv;
    }
}

MainActivity.java

package com.example.demo03_22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

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

        ListView lv=(ListView) this.findViewById(R.id.lv1);
        myAdapater myAdapater=new myAdapater(this, R.layout.item);
        lv.setAdapter(myAdapater);
    }
}

試験結果:

 改善:写真を追加

activity_main.xml  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:stretchColumns="2"
    >

    <ListView
        android:id="@+id/lv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

 item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_booknames"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <ImageView
        android:id="@+id/book_phone"
        android:layout_width="80dp"
        android:layout_height="80dp"

        />
    <TextView
        android:id="@+id/book_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        />



</LinearLayout>

 myAdapater.java

package com.example.demo03_22;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class myAdapater extends BaseAdapter {
    private BookItem[] books={new BookItem("陈某人",R.drawable.dog),
            new BookItem("周某人",R.drawable.dog),
            new BookItem("钟某人", R.drawable.dog),
            new BookItem("林某人",R.drawable.dog),
            new BookItem("涛某人",R.drawable.dog)};

    LayoutInflater inflater;
    int id_item;
    public myAdapater(Context context,int id_item){
        this.id_item=id_item;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return books.length;
    }

    @Override
    public Object getItem(int position) {
        return books[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")@Override
    public View getView(int position, View view, ViewGroup parent) {
        LinearLayout LL=(LinearLayout)inflater.inflate(id_item,parent,false)
                ;
        ImageView iv=(ImageView)LL.findViewById(R.id.book_phone);
        iv.setImageResource(books[position].photo);

        TextView tv;
        tv=(TextView)LL.findViewById(R.id.book_name);
        tv.setText(books[position].name);

        return LL;
    }
/**
 *  定义一个图片类
  */
    private class BookItem{
        String name;
        int photo;

        public BookItem(String name,int photo){
            this.name=name;
            this.photo=photo;
        }
    }
}

MainActivity.java

package com.example.demo03_22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

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

        ListView lv=(ListView) this.findViewById(R.id.lv1);
        myAdapater myAdapater=new myAdapater(this, R.layout.item);
        lv.setAdapter(myAdapater);
        lv.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView textView=(TextView)view.findViewById(R.id.book_name);
        String name=(String)textView.getText();
        String text="确定选择"+name+"今晚打火锅吗";
        Toast.makeText(this,text,Toast.LENGTH_LONG).show();
    }
}

おすすめ

転載: blog.csdn.net/m0_56233309/article/details/123657578