如何设置微调框列表值的第一位显示为空

时间:2018-12-13 10:42:05

标签: android spinner adapter

如何设置微调框列表的第一位置值显示为空,我附加了适配器和活动:请解决我的问题。

SpinnerAdapterIdtypeMaster

public class SpinnerAdapterIdtypeMaster extends ArrayAdapter {
    private Context mContext;
    private List<CMSSubMasterDB> spinnerList = new ArrayList<>();

    private MasterType type;

    public SpinnerAdapterIdtypeMaster(@NonNull Context context, List<CMSSubMasterDB> list) {
        super(context, 0, list);
        mContext = context;
        spinnerList = list;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public int getCount() {
        return spinnerList.size();
    }

    @Nullable
    @Override
    public CMSSubMasterDB getItem(int position) {
        return spinnerList.get(position);
    }

    @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.spinner_list, parent, false);

        CMSSubMasterDB currentMovie = spinnerList.get(position);


        TextView name = (TextView) listItem.findViewById(R.id.spinner);
        name.setText(currentMovie.name);


        return listItem;
    }


    public View getCustomView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        //return super.getView(position, convertView, parent);

        View listItem = convertView;
        if (listItem == null)
            listItem = LayoutInflater.from(mContext).inflate(R.layout.spinner_list, parent, false);

        TextView label = (TextView) listItem.findViewById(R.id.spinner);
        label.setText(spinnerList.get(position).name);


        return listItem;
    }
}

CreateRyotManagerFragment

public class CreateRyotManagerFragment extends Fragment {
    Spinner mIDType
    List<CMSSubMasterDB> mIdtypeMasterList;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.layout_new_ryot, container, false);
        mIDType = view.findViewById(R.id.idtype);
        mIdtypeMasterList = AppUtils.getIDTypeMasterDb();
        SpinnerAdapterIdtypeMaster idTypeMaster = new SpinnerAdapterIdtypeMaster(getActivity().getApplicationContext(), mIdtypeMasterList);
        mIDType.setAdapter(idTypeMaster);
        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

 Try this code,
          List<CMSSubMasterDB> mIdtypeMasterList = new List<CMSSubMasterDB>;
          CMSSubMasterDB cmsdb = new CMSSubMasterDB();
          cmsdb.setName("Select value");
          mIdtypeMasterList.add(cmsdb);
          List<CMSSubMasterDB> mIdList =AppUtils.getIDTypeMasterDb();
          for (int i=0;i<mIdList.size();i++){
             mIdtypeMasterList.add(mIdList.get(i));
           }
       SpinnerAdapterIdtypeMaster idTypeMaster = new 
       SpinnerAdapterIdtypeMaster(getActivity().getApplicationContext(), 
       mIdtypeMasterList);
       mIDType.setAdapter(idTypeMaster);

 mIDType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                                mgetdatacall(position);
                            }

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

                            }
                        });
private void mgetdatacall(int position) {
  if(position != 0) {
    mIdList.get(position - 1).getname();
   }
  }