更新时如何设置微调器的选定项目

时间:2017-09-08 05:56:23

标签: java android

例如,当我在微调器上选择客户名称并添加到列表视图中时。然后我去更新页面我看到微调器状态没有改变。更新时如何设置微调器状态?

     prepareData();

        NewSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                value = parent.getItemAtPosition(position).toString();
                custid = databaseHelper.getSpinnerCustId(value, user_id2);
                getIndex(NewSpinner, value);
                //NewSpinner.setSelection(((ArrayAdapter<String>)NewSpinner.getAdapter()).getPosition(value));
            }

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

 public void prepareData() {
        users = databaseHelper.getAllUsers(user_id2);
        adapter = new ArrayAdapter<String>(UpdateMobRepairActivity.this, android.R.layout.simple_dropdown_item_1line, android.R.id.text1, users);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        NewSpinner.setAdapter(adapter);
    }

    public void getIndex(Spinner spinner, String myString) {
        int index = 0;
        SpinnerAdapter adapter = spinner.getAdapter();

        for (int i = 0; i < adapter.getCount(); i++) {
            if (spinner.getItemAtPosition(i).equals(myString)) {
                spinner.setSelection(i);
                return;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

更改后通知适配器

public void getIndex(Spinner spinner, String myString) {
   int index = 0;
   SpinnerAdapter adapter = spinner.getAdapter();

   for (int i = 0; i < adapter.getCount(); i++) {
    if (spinner.getItemAtPosition(i).equals(myString)) {
        spinner.setSelection(i);
        return;
    }
   }

   adapter.notifyDataSetChanged();

}