如何使用SimpleAdapter在微调器中设置选择?

时间:2016-10-25 07:52:39

标签: android sqlite spinner simpleadapter

我从下面的代码填充微调器,它的工作完美。当用户从spinner中选择任何值时,我将在sqlite中保存stateCodeId。现在我想要的是当用户再来时,我想显示已经保存在sqlite中的ID的seleted值。如何显示在微调器中选择的值?

public void fillStateData() {
        try {
            State_data = new ArrayList<Map<String, String>>();
            State_data.clear();
            Cursor cursor_State = db.rawQuery("SELECT nSerialNo as _id,cCodeName FROM CodeMaster where nCtgId = 6", null);

            int i = 0;
            if (cursor_State.moveToFirst()) {
                do {
                    Map<String, String> datanum = new HashMap<String, String>();
                    if (i == 0) {
                        datanum.put("nStateID", "0");
                        datanum.put("cStateName", "Select State");
                        State_data.add(datanum);

                        datanum = new HashMap<String, String>();
                        datanum.put("nStateID", cursor_State.getString(0));
                        datanum.put("cStateName", cursor_State.getString(1));
                        State_data.add(datanum);
                    } else {
                        datanum.put("nStateID", cursor_State.getString(0));
                        datanum.put("cStateName", cursor_State.getString(1));
                        State_data.add(datanum);
                    }
                    i += 1;
                } while (cursor_State.moveToNext());

            }
            String[] fromwhere = {"nStateID", "cStateName"};
            int[] viewswhere = {R.id.txtnStateID, R.id.txtcStateName};
            StateAdapter = new SimpleAdapter(getActivity(), State_data, R.layout.state_list_template, fromwhere, viewswhere);
            spnState.setAdapter(StateAdapter);
            cursor_State.close();
            spnState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    TextView stateId = (TextView) view.findViewById(R.id.txtnStateID);
                    stateCodeId = stateId.getText().toString();

                    fillDistrictData(stateCodeId);
                }

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

                }
            });
        } catch (Exception e) {

            e.printStackTrace();

        }
    }

像这样我从sqlite获取ID。

Cursor c = db.rawQuery("SELECT nStateId from MemberMaster where nCustID ="+SesPMbrID+"", null);

        c.moveToFirst();
        String state = c.getString(0);

1 个答案:

答案 0 :(得分:0)

使用

spinner.setSelection(position);