单击时微调器选择的项目不显示

时间:2019-05-13 20:16:33

标签: java android mvvm spinner android-room

我添加了一个微调器,该微调器从房间数据库中检索数据 ,当我单击微调器时,它会正确显示可用的项目,但是当我选择一个时,它不会像所选的那样出现在微调器中,并且什么也不显示

我发现CollegeSpinner.setOnItemSelectedListener 即使我选择了某项,也不会触发

    ArrayAdapter<String> adapter;
    CollegeViewModel collegeViewModel;
    Spinner collegeSpinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_note);

        collegeSpinner = (Spinner) findViewById(R.id.spinner_college);
        final ArrayList<String> collegesList = new ArrayList<>();
        final CollegeAdapter collegeAdapter = new CollegeAdapter();
        collegeViewModel = ViewModelProviders.of(this).get(CollegeViewModel.class);
        collegeViewModel.getAllColleges().observe(this, new Observer<List<College>>() {
            @Override
            public void onChanged(List<College> colleges) {
                //this is what happens when when data changes // the code bellow should be related to changing the view
                collegeAdapter.setColleges(colleges);  // getting colleges from database and assigning it to collegeadapter
                for (int i = 0; i < collegeAdapter.getItemCount(); i++) { // accessing the adapter to add it's elements to collegeList
                    collegesList.add(collegeAdapter.getCollegeAt(i).getCollegeName());
                }
            }
        });
        adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.small_view, collegesList);
        collegeSpinner.setAdapter(adapter);
        collegeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                // this function doesn't work even when i select something
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // also doesn't work
            }
        });

    }

3 个答案:

答案 0 :(得分:1)

实际上我找到了解决方案,但我不知道它为什么起作用 但这与CollegeAdapter被声明为final的事实有关,但我必须将其声明为final,否则在onChanged()中会出现错误

我做了一个函数,将来自collegeAdapter的元素添加到collegeList 因此该函数将不是一个最终值的参数将collegeAdapter的值分配给一个新的collegeAdapter,并且可以像这样

这是我的实现方式

collegeViewModel.getAllColleges().observe(this, new Observer<List<College>>() {
            @Override
            public void onChanged(List<College> colleges) {
                //this is what happens when when data changes // the code bellow should be related to changing the view 
                collegeAdapter.setColleges(colleges);  // getting colleges from database and assigning it to collegeadapter
                addElements(collegeAdapter, collegesList); 
            }
        });

这是函数

public void addElements(CollegeAdapter collegeAdapter, ArrayList<String> collegesList) {
        for (int i = 0; i < collegeAdapter.getItemCount(); i++) {
            collegesList.add(collegeAdapter.getCollegeAt(i).getCollegeName());
        }
        collegeSpinner.setAdapter(adapter);
    }

答案 1 :(得分:0)

collegeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str = collegeSpinner.getSelectedItem().toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                String str = llist.get(0);
            }
        });

答案 2 :(得分:0)

转到http://www.jsonschema2pojo.org/,然后将源类型更改为json,将注释类型更改为Gson,然后在其中复制整个json,并在Java类中创建整个json模型