【问题标题】:At second approach, second spinner value is not changing在第二种方法中,第二个微调器值没有改变
【发布时间】:2019-09-02 05:25:44
【问题描述】:

我为部门和课程使用了两个微调器,我在其中设置了各个部门的课程。 当我再次尝试选择部门时,它会在第二个微调器中显示相应的课程,但不会更改第二个微调器的显示值。

我尝试清除我的字符串数组作为第二个选项,但它仍然无法正常工作。

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, depts);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
DeptSpin.setAdapter(adapter);
DeptSpin.setOnItemSelectedListener(this);
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, courses);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
courseSpin.setAdapter(adapter1);
courseSpin.setOnItemSelectedListener(this);

.

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    courses.clear();
    courses.add("Select Course");
    Cursor cursor = HostelDB.getCourse(position - 1);
    if (cursor.getCount() == 0) {
        Toast.makeText(this, "Data not found", Toast.LENGTH_SHORT).show();
    } else {
        cursor.moveToFirst();
        do {
            courses.add(cursor.getString(cursor.getColumnIndex("Name")));
        } while (cursor.moveToNext());
    }
    cursor.close();
}

【问题讨论】:

    标签: android android-spinner onitemselectedlistener


    【解决方案1】:

    添加所有项目后,您需要调用notifyDataSetChanged()

    courses.clear();
    courses.add("Select Course");
    
    ...
    // refresh the adapter
    adapter1.notifyDataSetChanged();
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多