0

I have used two spinners for dept and course in which I am setting courses of respective depts. When I try to select department again it is showing respective courses in the second spinner but not changing the displayed value of the second spinner.

I tried clearing my string array as a second option but it's still not working.

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();
}
0

You need to call the notifyDataSetChanged() after you have add all the items:

courses.clear();
courses.add("Select Course");

...
// refresh the adapter
adapter1.notifyDataSetChanged();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.