java 如何为微调器设置适配器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13311279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:15:16  来源:igfitidea点击:

how to set adapter for spinner

javaandroid

提问by user1698448

how to set adapter to spinner if i have ArrayList<HashMap<String, String>>coursesList that contine information that i will display it

如果我有ArrayList<HashMap<String, String>>课程列表,如何将适配器设置为微调器,我将显示它的信息

what is the correct type of adapter that i should use

我应该使用的正确类型的适配器是什么

回答by Stephane Mathis

ArrayAdapter should work. Try with a string[].

ArrayAdapter 应该可以工作。尝试使用字符串 []。

Spinner s = null;
final String[] choices = {"1","2"};
ArrayAdapter<String> a =new ArrayAdapter<String>(ObservationSubmit.this,android.R.layout.simple_spinner_item, choices);
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(a); 

回答by Nidhi Bhatt

 String[] arraySpinner = new String[] {
            "1", "2", "3", "4", "5", "6", "7"
        };  
  Spinner s = (Spinner) findViewById(R.id.Spinner01);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, arraySpinner);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);

回答by PRATEEK BHARDWAJ

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.agreeta.firebasedatabasedemo.R;

import java.util.ArrayList;
import java.util.HashMap;

public class TestAdapter extends ArrayAdapter {

    private Context mContext;
    private ArrayList<HashMap<String, String>> hashMapArrayList = new ArrayList<>();

    public TestAdapter(@NonNull Context context, ArrayList<HashMap<String, String>> list) {
        super(context, 0 , list);
        mContext = context;
        hashMapArrayList = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if(listItem == null)
            listItem = LayoutInflater.from(mContext).inflate(R.layout.like_row,parent,false);

        TextView release = (TextView) listItem.findViewById(R.id.tv_title);
        release.setText(hashMapArrayList.get(position).get("key"));



        return listItem;
    }
}

Try with this example. Here "key" is your HashMap key by which you can set value to row of spinner.

试试这个例子。这里的“键”是您的 HashMap 键,您可以通过它为微调器行设置值。