选定的项目未在微调器 kotlin 中显示

Posted

技术标签:

【中文标题】选定的项目未在微调器 kotlin 中显示【英文标题】:Selected item not showing in spinner kotlin 【发布时间】:2018-08-15 04:30:47 【问题描述】:

下面是spinner的xml声明:

<Spinner
android:id="@+id/spnReasonActApplyLeave"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_
android:layout_
android:layout_marginTop="@dimen/dimen_20_dp" />

下面是activity类中的初始化:

var adapterReasons: LeaveReasonSpinnerAdapter ?= null    
adapterReasons = LeaveReasonSpinnerAdapter(this,R.layout.row_reason_spinner_item, reasonList)   
 spnReasonActApplyLeave.adapter = adapterReasons

spnReasonActApplyLeave.setOnItemSelectedListener(object: AdapterView.OnItemSelectedListener 
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) 
                Log.e("ApplyLeave", "Selected Reason: "+reasonList.get(position))
            

            override fun onNothingSelected(parent: AdapterView<*>?) 

            
        )

下面是row_reason_spinner_item xml文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tvReasonRowReasonSpinner"
    android:layout_
    android:layout_
    android:textColor="@color/black"
    android:textSize="@dimen/txt_16_sp" />

下面是 LeaveReasonSpinnerAdapter 类:

public class LeaveReasonSpinnerAdapter extends ArrayAdapter<LeaveReasonListPayloadDataResponse> 

    private final LayoutInflater mInflater;
    private Context mContext;
    int layoutResourceId;
    private List<LeaveReasonListPayloadDataResponse> data = new ArrayList<>();

    public LeaveReasonSpinnerAdapter(Context context, int resource, ArrayList<LeaveReasonListPayloadDataResponse> list) 
        super(context, resource, list);
        mContext = context;
        mInflater = LayoutInflater.from(context);
        layoutResourceId = resource;
        data = list;
    

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) 
        return createItemView(position, convertView, parent);
    

    @Override
    public int getCount() 
        return data.size();
    

    @Override
    public LeaveReasonListPayloadDataResponse getItem(int position) 
        return data.get(position);
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
        return createItemView(position, convertView, parent);
    

    private View createItemView(int position, View convertView, ViewGroup parent)
        View view = mInflater.inflate(layoutResourceId, parent, false);

        TextView label = (TextView) view.findViewById(R.id.tvReasonRowReasonSpinner);

        LeaveReasonListPayloadDataResponse offerData = data.get(position);

        label.setText(offerData.getReason());
        label.setTextColor(ContextCompat.getColor(mContext, R.color.black));
//        label.setBackgroundColor(ContextCompat.getColor(mContext, R.color.black));

        return view;
    

我尝试了很多东西。我还实现了 appcompatspinner 但仍然没有运气。当我选择微调器时,我可以在下拉列表中看到值,但在选择任何项目后看不到。

其中还有另一个问题。该微调器的 setonitemselectedlistener 也不会被调用。

以下是输出:

【问题讨论】:

你实现了spinner的onItemSelected吗? @HemantParmar 是的...我没有在代码中提到但是是的,我也实现了。 请分享代码。 @HemantParmar 检查更新的代码。我添加了该代码并显示了输出。 我检查了代码一切正常,您必须更改 onItemSelected 的实现,如 this 【参考方案1】:

当您绑定适配器时,该时间适配器具有值,然后它会显示在微调器中。例如使用下面的代码:

   var adapterReasons: LeaveReasonSpinnerAdapter?= null
    var reasonList= arrayListOf<LeaveReasonListPayloadDataResponse>() // bind your any type of data
    // used below code in for loop
    var data=LeaveReasonListPayloadDataResponse().apply 
        reason="Hello"
    
    var data2=LeaveReasonListPayloadDataResponse().apply 
        reason="Hello2"
    

    reasonList.add(data)
    reasonList.add(data2)
    adapterReasons = LeaveReasonSpinnerAdapter(this,
            R.layout.row_layout, reasonList)
    var SpinnerValue:Spinner=findViewById(R.id.spnReasonActApplyLeave)
    SpinnerValue.adapter = adapterReasons
    SpinnerValue.onItemSelectedListener = object : OnItemSelectedListener 
        override fun onItemSelected(parentView: AdapterView<*>, selectedItemView: View, position: Int, id: Long) 
            Toast.makeText(applicationContext,""+SpinnerValue.selectedItemPosition.toString(),Toast.LENGTH_SHORT).show()
        

        override fun onNothingSelected(parentView: AdapterView<*>) 
        

    

在您的活动中使用此代码onCreate()

【讨论】:

以上是关于选定的项目未在微调器 kotlin 中显示的主要内容,如果未能解决你的问题,请参考以下文章

jQuery Ajax 微调器未在 IE7 中显示

获取微调器选定项目文本?

获取微调器选定项目文本?

Android SQLite 在选定的微调器上显示特定行

在我的微调器 POJO 中,我有 id 和 name。所以在按钮单击时,我想在 kotlin android 中发送我的微调器 ID 而不是名称

Paypal 页面未在 Android 应用程序中加载,仅显示微调器,直到我触摸屏 6.0.1