am trying to use a spinner within a fragment to categorize the output of my recycler view
when I call the recycler view function from the onCreateView it works perfectly. however, when I call it from the spinner function, it requires 2 clicks to display.
here is the spinner XML code:
<Spinner
android:id="@ id/med_spinner"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_below="@ id/tv_med2"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/tv_spinner"
android:dropDownWidth="match_parent"
android:focusable="true"
android:gravity="center"
android:entries="@array/medicine"
/>
and here is the fragment:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_medicine, container, false);
RecyclerView rv_med = view.findViewById(R.id.rv_medicine);
db = new UserDB(getActivity());
med_name = new ArrayList<>();
med_type = new ArrayList<>();
med_type2 = new ArrayList<>();
//spinner declartion
spinner = view.findViewById(R.id.med_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.medicine, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
//recycler view adapter and layout manger
recyclerViewAdapter = new RecyclerViewAdapterMedicine(getContext(), med_name, med_type, med_type2);
rv_med.setAdapter(recyclerViewAdapter);
rv_med.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
and here is the spinner functions
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
med_name.clear();
med_type.clear();
med_type2.clear();
cata = spinner.getSelectedItem().toString();
display(cata);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
cata = "All";
display(cata);
}
here's the display function:
void display(String type) {
db.open();
Cursor cursor = db.listMedicine();
if (cursor.getCount() == 0) {
Toast.makeText(getActivity(), "an error occurred", Toast.LENGTH_LONG).show();
} else {
if (type.equals("All")){
while (cursor.moveToNext()) {
med_name.add(cursor.getString(1));
med_type.add(cursor.getString(2));
med_type2.add(cursor.getString(3));
}
}else{
while (cursor.moveToNext()) {
if(cursor.getString(2).equals(type) || cursor.getString(3).equals(type)) {
med_name.add(cursor.getString(1));
med_type.add(cursor.getString(2));
med_type2.add(cursor.getString(3));
}
}
}
db.close();
}
}
I've tried to put the spinner functions within a spinner.onItemSelectedListener inside and outside the fragment, I tried to enable and disable focus and focusableTochMode, I also tried to call the recycler view before the spinner in the fragment.
CodePudding user response:
You need to notify your RecyclerViewAdapter
that data has changed. Add
recyclerViewAdapter.notifyDataSetChanged()
at the end of your display
.