如何根据数据库值从android Spinner中选择的值进行设置?

时间:2018-09-21 10:23:50

标签: android android-spinner

如何从数据库中设置微调框选择的值,我附加了以下代码并附加了微调框,该字符串数组仅从数据库和同一微调框保存以设置所选的字符串数组,请帮帮我。

    import android.app.AlertDialog;
    import android.app.DatePickerDialog;
    import android.content.DialogInterface;
    import android.net.Uri;

    public class Doc40Fragment extends Fragment   {
  private Spinner mestimationspin, Uomspin;
      private String[] yesNo = new String[]{"yes", "no"};
        private String[] avareage = new String[]{"GOOD", "POOR", "AVERAGE"};
        private String[] fitUnfit = new String[]{"Fit", "Unfit"};
        private String[] uom = new String[]{" ", "Nos", "Tonnes"};
        private String[] ras = new String[]{"Field wetness", "Low earthing up", "Power line crossing", "Ryot unwilling", "Small plot"};
      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_doc40, container, false);
 Uomspin = view.findViewById(R.id.uom_edit);

     Uomspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    adapterView.getItemAtPosition(i);

                    String dist_pos = "uom" + i;
                    int resID = getId(dist_pos, R.array.class);
                    final String[] uom = view.getContext().getResources().getStringArray(resID);
                    ArrayAdapter<String> uobAdater = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, uom);
                    Uomspin.setAdapter(uobAdater);

                }

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

                }
            });

      return view;
        }

1 个答案:

答案 0 :(得分:0)

     private void SpinnerSetup() {
        adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.india_states, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mStateSpinner.setAdapter(adapter);

        mStateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
                String Select_State = "Select State";
                if (!Select_State.matches(adapterView.getItemAtPosition(pos).toString())){
                    state = adapterView.getItemAtPosition(pos).toString();
                  }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }



     if (!json_data.isNull("state"))
 {
    User_sate = json_data.getString("state").toString();
    int spinnerPosition = adapter.getPosition(User_sate);
    mStateSpinner.setSelection(spinnerPosition);
    }