0

I am creating a dynamic view which contains TextView and a Spinner. I' ve an array of items which has to be shown in the spinner. Now, for an instance, SpinnerA shows item1 from the list so I don't want SpinnerB to show item1 for the selection. Similarly, if SpinnerB has selected item3 then SpinnerA should not show item3 for the selection. Finally precisely and concisely what I want is, the item already selected by one spinner should not be visible for selection by any other spinner.

Edit: I am receiving a JSON response which is having a count of products. On that basis, I'll be creating dynamic views containing spinner and auto-populating the values of product into the spinner. Also on click of adding more button, I will be facilitated by adding more dynamic views. So now the scenario is the preselected value in one spinner should be visible for selection in any other spinner. I've mentioned an image URL of my layout below.the layout link Kindly, help me with the logic.

Android1005
  • 155
  • 3
  • 14

2 Answers2

0

I asume single spinner have single listener if not u have to add one more swich

    @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if(spinnerTextFromFirst ==String.valueOf(parent.getItemAtPosition(position));

}

If u want just to now show those item u need to add list of integers and reload adpters with one more condition

        for (int j = 0; j < listFromAdapter.length - 1; j++) {
        for (int i = 0; i < list.length - 1l i++){
            if (list[i])!=j){
                addToNewListWhichWillBeYourAdapter();
            }
        }
    }
ppqq
  • 28
  • 7
  • Hi, thanks for your reply but I will be creating spinners depending on my json response which can be ranging from 0 to 20 spinners, so in that case, how will I be checking the value – Android1005 Jun 01 '18 at 11:02
0

The simplest way is by refreshing the items from spinnerB when spinnerA item is selected.

final List<String> items = new ArrayList<String>();
items.add("one");
items.add("two");
items.add("three");

final List<String> itemsB = new ArrayList<String>(items);
final List<String> itemsA = new ArrayList<String>(items);

ArrayAdapter<String> adapterA = new ArrayAdapter<String>(this,
     android.R.layout.simple_spinner_item, itemsA);

ArrayAdapter<String> adapterA = new ArrayAdapter<String>(this,
     android.R.layout.simple_spinner_item, itemsB);

boolean isFirstCallSpinnerA = true;
boolean isFirstCallSpinnerB = true;

spinnerA.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      // onItemSelected is always called when OnItemSelectedListener created.
      // so, ignore first call.
      if(isFirstCallSpinnerA) {
        isFirstCallSpinnerA = false;
        return;
      }

      // clear spinner items b
      // then add again
      itemsB.clear();
      itemsB.addAll(items); 
      itemsB.remove(position);
      adapterB.notifyDataSetChanged();
    } 

    public void onNothingSelected(AdapterView<?> adapterView) {
        return;
    } 
}); 

spinnerB.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      if(isFirstCallSpinnerB) {
        isFirstCallSpinnerB = false;
        return;
      }

      // clear spinner items A
      // then add again
      itemsA.clear();
      itemsA.addAll(items); 
      itemsA.remove(position);
      adapterA.notifyDataSetChanged();
    } 

    public void onNothingSelected(AdapterView<?> adapterView) {
        return;
    } 
}); 

I can't confirm that the above code will work. You also need to temporary hold the selected item of spinnerA and spinnerB in case you want to retain the selected item.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • Hi, thanks for making reply but my doubt is : I am generating dynamic view based on json response and accordingly number of spinners can be generated ranging from 0 to 20 – Android1005 Jun 01 '18 at 11:05