Android : Spinner(列表选项框) + BaseAdapter -简单应用

 ​​容器与适配器:​​​​​        http://t.csdnimg.cn/ZfAJ7

示例图:

Android : Spinner(列表选项框) + BaseAdapter -简单应用_第1张图片

实体类 Demo.java

package com.example.mygridviewadapter.entity;

public class Demo {
    private String  text;
    private  int img;

    public Demo(String text, int img) {
        this.text = text;
        this.img = img;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }
}

适配器 写个类继承BaseAdapter 

package com.example.myspinnerbaseadapter;

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.TextView;

import com.example.myspinnerbaseadapter.entity.Data;

import java.util.List;

public class DataAdapter extends BaseAdapter {
    private List dataList;
    private Context context;

    static class ViewHoler{
        ImageView imageView;
        TextView textView;
    }
    //BaseAdapter最基本的几个方法:
    // 1. getCount 填充的数据集数
    // 2.getItem 数据集中指定索引对应的数据项
    // 3. getItemId 指定行所对应的ID
    // 4. getView 每个Item所显示的类容

    public DataAdapter(List dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoler holer = null;
        if(holer == null){
            holer = new ViewHoler();

            //获取 自定义view布局 获取相关属性
            convertView = LayoutInflater.from(context).inflate(R.layout.spinner_view,parent,false);
            holer.imageView = convertView.findViewById(R.id.bntImg);
            holer.textView = convertView.findViewById(R.id.btnTitle);


            convertView.setTag(holer);

        }else {
            holer = (ViewHoler)convertView.getTag();

        }

        //设置值
        holer.imageView.setBackgroundResource(dataList.get(position).getImg());
        holer.textView.setText(dataList.get(position).getTitile());

        return convertView;
    }
}

MainActivity.java

package com.example.myspinnerbaseadapter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myspinnerbaseadapter.entity.Data;

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

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

    //组件
    private Spinner spinnerOne,spinner;
    private List dataList =null;

    private Context context;
    private DataAdapter dataAdapter =null;

    //图片
    private int[] arr = {
            R.mipmap.a,
            R.mipmap.b,
            R.mipmap.c,
            R.mipmap.d,
            R.mipmap.e,
            R.mipmap.f,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //上下文
        context = this;

        spinnerOne = findViewById(R.id.tbnSpiOne);
        spinner = findViewById(R.id.tbnSpi);

        dataList = new ArrayList<>();
        //循环传入数据
        for (int i = 0 ; i < arr.length;i++){
            dataList.add(new Data("样式"+i,arr[i]));
        }

        dataAdapter = new DataAdapter(dataList,context);

        // 往容器中设置适配器
        spinner.setAdapter(dataAdapter);

        //事件
        spinnerOne.setOnItemSelectedListener(this);
        spinner.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView parent, View view, int position, long id) {
            if(parent.getId() == R.id.tbnSpiOne){
                Toast.makeText(this,"您选择了"+parent.getItemAtPosition(position),Toast.LENGTH_SHORT).show();
            } else if (parent.getId() == R.id.tbnSpi) {
                TextView textView = findViewById(R.id.btnTitle);
                Toast.makeText(this,"您选择了"+textView.getText().toString(),Toast.LENGTH_SHORT).show();

            }
    }

    @Override
    public void onNothingSelected(AdapterView parent) {

    }
}

activity_main.xml




    

    

    


    

    -->
    

spinner_view.xml




    
    
    
    


你可能感兴趣的:(Android,相关,android)