【Android】AlertDialog与ListAdapter

文章目录

  • 一、setAdapter()
  • 二、ArrayAdapter
    • 1.简单的String
    • 2.自定义
  • 三、SimpleAdapter
    • 1.自定义


一、setAdapter()

在这里插入图片描述
使用的Adapter是ListAdapter类。
ListApdater的子类是ArrayAdapter、SimpleAdapter、BaseAdapter。

PS:RecyclerAdapter不行,不是继承的子类。

二、ArrayAdapter

1.简单的String

package com.example.hello;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


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

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Title");
        final String[] fruits={"apple","banana"};
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,fruits);
        builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,fruits[i],Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
}

【Android】AlertDialog与ListAdapter_第1张图片

2.自定义

【Android】AlertDialog与ListAdapter_第2张图片

四步:

  • 定义Fruit类
  • 定义Item的布局
  • 定义继承ArrayAdapter的FruitAdapter
  • MainActivity.java中创建,之后就是绑到builder上。

定义Fruit类

package com.example.hello;

public class Fruit {
    private String mName;
    private int mImageId;

    public Fruit(String name,int ImageId)
    {
        mName=name;
        mImageId=ImageId;
    }

    public String getmName()
    {
        return mName;
    }

    public int getmImageId()
    {
        return mImageId;
    }
}

定义Item的布局:layout.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fruit_image"/>

LinearLayout>

定义继承ArrayAdapter的FruitAdapter

package com.example.hello;

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

import java.util.List;

public class FruitAdapter extends ArrayAdapter<Fruit> {
    private int mresource;

    public FruitAdapter(Context context, int resource, List<Fruit> objects) {
        super(context, resource, objects);
        mresource=resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit=getItem(position);
        View view= LayoutInflater.from(getContext()).inflate(mresource,parent,false);
        ImageView fruitImage=view.findViewById(R.id.fruit_image);
        TextView fruitText=view.findViewById(R.id.fruit_name);
        fruitImage.setImageResource(fruit.getmImageId());
        fruitText.setText(fruit.getmName());
        return view;
    }
}

MainActivity.java中创建

package com.example.hello;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


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

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Title");
        List<Fruit> data = new ArrayList<>();
        data.add(new Fruit("banana",R.drawable.banana));
        data.add(new Fruit("watermelon",R.drawable.watermelon));
        FruitAdapter fruitAdapter=new FruitAdapter(
                MainActivity.this,
                R.layout.layout,
                data);
        builder.setAdapter(fruitAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,""+i,Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
}

三、SimpleAdapter

1.自定义

【Android】AlertDialog与ListAdapter_第3张图片
两步:

  • 定义Item的布局
  • MainActivity.java中创建SimpleAdapter,之后就是绑到builder上。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fruit_image"/>

LinearLayout>

package com.example.hello;

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


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

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Title");
        
        final List<Map<String,Object>> list=new ArrayList<>();
        final int price[]={R.drawable.banana,R.drawable.watermelon};
        final String[] name={"banana","watermelon"};
        for(int i=0;i<name.length;i++)
        {
            Map<String,Object> map=new HashMap<>();
            map.put("name",name[i]);
            map.put("price",price[i]);
            list.add(map);
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(
                MainActivity.this,
                list,
                R.layout.layout,
                new String[]{"name","price"},
                new int[]{R.id.fruit_name,R.id.fruit_image}
        );
        builder.setAdapter(simpleAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this,name[i],Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }
}

你可能感兴趣的:(Android)