片段内部对话框中的Android Spinner未显示所选值

问题描述 投票:0回答:1

我已经在Fragment中创建了一个包含微调器的对话框,它在下拉列表中显示值,但不显示所选值。请参见下面的代码。

AppDialog.showUnlockAssignVehichleDialog(getActivity(), assignedVehicleParentBean.vehicleList, ReservationCarClassBookedFragment.this);

对话代码

List<Long> mUnlockAssignVehicleIDList = new ArrayList<>();
List<String> mUnlockAssignVehicleList = new ArrayList<>();
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_unlock_assign_vehicle);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = (int) (activity.getResources().getDisplayMetrics().widthPixels - activity.getResources().getDimension(R.dimen.d_10dp));
dialog.getWindow().setAttributes(lp);
dialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
final Spinner spinner = dialog.findViewById(R.id.spn_vehicleList);
mUnlockAssignVehicleList.add(0, "Select One...");
mUnlockAssignVehicleIDList.add(0, 0000L);
if (vehicleList != null && vehicleList.size() > 0) {
        for (AssignedVehicleBean bean : vehicleList) {
            mUnlockAssignVehicleList.add(bean.plate_no + "-" + bean.model);
            mUnlockAssignVehicleIDList.add(bean.id);
        }
 ArrayAdapter arrayAdapter = new ArrayAdapter<>(activity, R.layout.spinner_item, mUnlockAssignVehicleList);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);
        arrayAdapter.notifyDataSetChanged();
    }
    dialog.show();

还有spinner_item文件

   <?xml version="1.0" encoding="utf-8"?>
   <TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="8dp"
   android:textColor="@color/c_black"
   android:textSize="14sp" />

注:-如我所见,上下文存在问题,我不知道如何正确使用它,例如当我从xml提供值时,它将显示它。谢谢

android android-studio dialog spinner android-spinner
1个回答
0
投票

setOnItemSelectedListener在您的spinner上丢失:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String spinnerItem = (String) spinner.getItemAtPosition(position);
           //now you have value do something dismiss dialog etc.
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.