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

时间:2018-03-07 06:40:08

标签: android kotlin android-arrayadapter android-spinner

下面是微调器的xml声明:

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

以下是活动类中的初始化:

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_width="match_parent"
    android:layout_height="wrap_content"
    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。

以下是输出: enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

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

   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()中使用此代码。