gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:01 31 4
gpt4 key购买 nike

下面是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" />

下面是 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_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

最佳答案

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

   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<*>) {
}

}

在您的 Activity onCreate() 中使用此代码。

关于android - 所选项目未在微调器 kotlin 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49145694/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com