3

I have added Spinner inside RecyclerView , when i am trying to get spinner selected item data, its getting another/wrong position data, any one suggest me to get correct selected item and position from Spinner onItemSelected

Here is my code

@Override
public void onBindViewHolder(final QuestionHolder holder, final int position) {
        if (position % 2 == 1)
            holder.itemView.setBackgroundColor(Color.parseColor("#F8F8F8"));
        adapter = new ArrayAdapter<Option>(binding.getRoot().getContext(),
                        R.layout.item_spinner, questionList.get(position).getOptions());
        adapter.setDropDownViewResource(R.layout.item_spinner);
        binding.optionSpinner.setAdapter(adapter);

        binding.serialNo.setText((position + 1) + ".");
        binding.setQuestion(questionList.get(position));
        binding.executePendingBindings();
        binding.optionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(holder.itemView.getContext(), position+" : "+binding.optionSpinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
                spinnerData.setSelectedData(position, binding.optionSpinner.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
srinu
  • 266
  • 1
  • 5
  • 18
  • 1
    call `setOnItemSelectedListener` inside your custom `QuestionHolder`, not inside `onBindViewHolder` method, also do not create `ArrayAdapter` there - do that in `QuestionHolder` too – pskink Nov 15 '17 at 08:48

2 Answers2

4

I think you have to try this

  showSpinnerList.setOnItemSelectedListener(new 
         AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int 
               position, long id) {
                // On selecting a spinner item

                String item = parent.getItemAtPosition(position).toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // todo for nothing selected
            }
        });
Sandeep Sankla
  • 1,250
  • 12
  • 21
1

Check this, can be helpful and may fix your problem. If this won't fix your problem at least you get rid of Lint error. Lint error “Do not treat position as fixed; only use immediately…”. So everywhere you are using final int position** change it to getAdapterPosition();

The documentation of RecyclerView.Adapter.onBindViewHolder() states:

Note that unlike ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. If you need the position of an item later on (e.g. in a click listener), use getAdapterPosition() which will have the updated adapter position

So, technically items may be re-arranged and binding will not be necessary because items are not invalidated yet. The position variable received holds true only for the scope of bind function and will not always point to correct position in the data set. That's why the function getAdapterPosition() must be called everytime updated position is needed.

IMHO, mLastPosition = holder.getAdapterPosition(); is still potentially wrong. Because item may be re-arranged and mLastPosition still points to old position.

About why lint is silent, perhaps Lint rule is not that thorough. Its only checking whether position parameter is being copied or not.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
svkaka
  • 3,942
  • 2
  • 31
  • 55