Java – dynamic spinners – if you select an item from one spinner, hide it from the other spinners – Android
How do I hide items in other spinners currently selected in one spinner?
I've tried to delete the item through the ArrayList string and arrayadapters, but I noticed that once it was deleted from the list, I chose not to reference the list item anymore (because it no longer exists)
Now suppose I have four dynamically created spinners, all of which have the same ArrayList as their resources. Now I want to use this adapter to get the position of the selected item from one spinner, and then hide it from the other three spinners
for (int i = 0; i < numberOfStops; i++) {
AddStopView stopView = new AddStopView(getActivity());
stopView.setCallback(BaseBookingFragment.this);
stopView.setPassengerNames(extraPassengerNames);
stopViews.add(stopView);
parent.addView(stopView,viewPosition);
}
In the above code, I create stop views dynamically, and each stop view has a passenger name spinner And all of these spinners have the same ArrayList as their assets
AddStopView. A piece of code in Java
public AddStopView(Context context) {
super(context);
initialize();
}
public void setCallback(StopViewCallback callback) {
this.callback = callback;
}
public void setPassengerNames(List<String> passengerNames) {
this.passengerNames = passengerNames;
passengerAdapter.setNames(passengerNames);
}
private void initialize() {
inflate(getContext(),R.layout.view_stop,this);
passengerAdapter = new ExtraPassengerAdapter(getContext());
passengerAdapter.setNames(passengerNames);
nameSpinner.setAdapter(passengerAdapter);
nameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
if (view == null) {
return;
}
passengerName = (String) view.getTag();
if (position != 0)
callback.updatePassengerList(AddStopView.this,(position - 1));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
nameSpinner. Callback code for setonitemselectedlistener
@Override
public void updatePassengerList(AddStopView addStopView,int position) {
for (String passName : extraPassengerNames) {
if (addStopView.getpassengerName().equals(passName)) {
extraPassengerNames.remove(passName);
break;
}
}
for (AddStopView stopView : stopViews) {
if (!stopView.equals(addStopView))
stopView.setPassengerNames(extraPassengerNames);
}
}
From extrapassengeradapter Java code
public class ExtraPassengerAdapter extends BaseAdapter {
private List<String> names = new ArrayList<>();
private Context context;
public ExtraPassengerAdapter(Context context) {
this.context = context;
names.add(get0Position());
}
public void setNames(List<String> names) {
this.names.clear();
this.names.add(get0Position());
this.names.addAll(names);
notifyDataSetChanged();
}
@Override
public int getCount() {
return names.size();
}
@Override
public String getItem(int position) {
return names.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_stop,parent,false);
String name = getItem(position);
textView.setText(name);
textView.setTag(name);
return textView;
}
private String get0Position() {
return context.getString(R.string.passenger_name);
}
}
Solution
I think the correct way to solve the problem is to create a list of all possible options and 4 lists of 4 adapters for each spinner After selecting something, update each list according to the logic you describe, and call adapter for each adapter notifyDataSetChanged().
