0

I am trying to get the index of a spinner that is inside my inflated view.

Although I'm successful in retrieving the index of my child view when it's inflated on a button click like this:

itineraryDetailLL = (LinearLayout) findViewById(R.id.itineraryDetailLinearlayout);
childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);

int posValue = itineraryDetailLL.indexOfChild(childView);
Toast.makeText ( PlanItineraryDetailView.this, Integer.toString(posValue), Toast.LENGTH_SHORT ).show();

This bit of code returns me 0 for 1st inflated view, 1 for 2nd inflated view and so on...

Problem

But when I'm trying to get the index position of the spinner (by calling the setOnTouchListener event) in each of my child view it returns me the last index position for each spinner

spinnerPlanItinerary.setOnTouchListener(new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int indexValueTown = itineraryDetailLL.indexOfChild(childView);
            Toast.makeText ( PlanItineraryDetailView.this, Integer.toString(indexValueTown), Toast.LENGTH_SHORT ).show();
            return true;
        }

    });

This returns me 1 for spinner in my 1st inflated view, 1 for spinner my 2nd inflated view (It should return 0 for spinner in my 1st inflated view, 1 for spinner in my 2nd inflated view).

I hope I'm clear with my problem.

Note I'm inflating only two views only for testing right now.

  • use onitem clicklistener for the spinner item touch event. – Kevan Aghera Sep 12 '17 at 11:02
  • I've done that too but the problem persists. –  Sep 12 '17 at 11:04
  • 1
    are itineraryDetailLL & childView global variable? if so, then make them local variable & final. something like: final LinearLayout itineraryDetailLL = (LinearLayout) findVi.... & see – Sandip Fichadiya Sep 12 '17 at 11:05
  • what exactly u want? – Kevan Aghera Sep 12 '17 at 11:06
  • you can use setonitemselectedlistner to get selected item position from spinner. – Narendra Kumar Sep 12 '17 at 11:10
  • @kevanaghera, I have two inflated card view and each card has one spinner. Now, clicking the spinner in 1st inflated card view should return 0 and on the 2nd one, it should return 1. –  Sep 12 '17 at 11:11
  • u want to spinner item position or cardview item position? – Kevan Aghera Sep 12 '17 at 11:12
  • @NarendraKumar, I'm not trying to get the item position but the position of the spinner itself. –  Sep 12 '17 at 11:12
  • @kevanaghera, I want the card view position on my spinner click event. is it possible to get the card view item position on spinner click? If it is you'll save my day! –  Sep 12 '17 at 11:14
  • 1
    @SafanMomin Check This Link i think its working. https://stackoverflow.com/questions/27886737/recyclerview-get-position-and-switch-to-activity/27886776#27886776 Second One is :- https://stackoverflow.com/questions/35607524/click-an-imagebutton-which-belongs-to-a-cardview-inside-a-recyclerview – Kevan Aghera Sep 12 '17 at 11:18
  • @SafanMomin could you please tell me is "itineraryDetailLL" global variable? – Sandip Fichadiya Sep 12 '17 at 11:23
  • @sam_0829, Yes they were final, I made them local variables and final and it worked like charm. Thanks, alot dude! –  Sep 12 '17 at 11:27
  • @kevanaghera, Thanks for the links but they were using recycler view for their inflated view. For some reason, I have to stick with my linear layout. It did work by making my global variables local and final as suggested and it worked. Thanks for your for inputs! :) –  Sep 12 '17 at 11:31
  • @SafanMomin Welcome Bro. – Kevan Aghera Sep 12 '17 at 11:39

1 Answers1

1

Posting as answer so someone who stumble here might get solution.

The problem in your case was your itineraryDetailLL & childView were global variables. That means whenever you add new item via button click, there value was updating to the last inflated view. now whenever you touch on Spinner, you were getting the last index as expected.

So making them local & final variable made sure that you refer to the same view when you touch the spinner.

I didn't understood your requirement or what you were trying to do fully though. There might be better way of achieving this.

Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47