Dynamic Spinners - if an item is selected from one counter, hide it from other spinners - Android

How to hide an item from other spinners that is currently selected on the same counter?

I tried to remove items through an ArrayList of strings and ArrayAdapters, but I noticed as soon as it was removed from the list, the selection no longer refers to the list item (because it no longer exists).

Now suppose I have 4 spinners that are created dynamically, and they all have the same ArrayList as their resource, and now I would like to use this adapter to extract the position of the selected item from 1 spinner, and then hide it from 3 other 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 dynamically create Stop Views and each Stop View has a passenger name counter. And all these spinners have the same ArrayList as their resource.

piece of code from AddStopView.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.setOnItemSelectedListener callback code

@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);
    }
}

code from ExtraPassengerAdapter.java

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

}

+4
source share
1 answer

I think the right way to solve your problem is to create one list with all possible parameters and 4 lists with 4 adapters for each counter. When something is selected, you update each list in accordance with the logic you describe and call adapter.notifyDataSetChanged()for each adapter.

0
source

Source: https://habr.com/ru/post/1661151/


All Articles