Spinner in Fragment

1

I have a Spinner in a Fragment , that when I click on an item of Spinner leads me to another Fragment . But when I run the app, it goes directly to the first item in Fragment . It does not show me the Fragment that has the Spinner , but it opens the first option of Spinner .

I add the code of Spinner

public class Bajar_Peso extends Fragment {

public Bajar_Peso() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view;
    view = inflater.inflate(R.layout.fragment_bajar__peso, container, false);

    Spinner spinnerA , spinnerB;
    String[] Frutas;
    String[] Vegetales;

    Button secreto;

    secreto=(Button)view.findViewById(R.id.btn_secreto);

    spinnerA = (Spinner)view.findViewById(R.id.spinner_Frutas);
    spinnerB=(Spinner)view.findViewById(R.id.spinner_Vegetales);

    Frutas = getResources().getStringArray(R.array.Bjar_PesoA);
    Vegetales=getResources().getStringArray(R.array.Bjar_PesoB);

    //SPINNER PARA LAS FRUTAS.
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1,Frutas);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerA.setAdapter(arrayAdapter);

    //SPINNER PARA LOS VEGETALES
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,Vegetales);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerB.setAdapter(adapter);


    //SELECCION DE LAS FRUTAS
           //SELECCION DE LOS VEGETALES
    spinnerB.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {


           if(position==0)
           {
               getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_main,new BP_Zanahorias()).commit();
           }


        }

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

        }
    });


    secreto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    return view ;
}
}
    
asked by Sergio 24.01.2017 в 02:53
source

2 answers

1

Your problem is that onItemSelected also runs when the view is created. A simple way to remedy that would be to add a boolean type marker in the fragment and ignore the first call:

boolean inicio = true;

and in the listener:

       if(position==0)
       {
           if (!inicio){
               getActivity().getSupportFragmentManager().
               beginTransaction().
               replace(R.id.content_main,new BP_Zanahorias()).commit();
           } else {
               inicio=false;
           }
       }
    
answered by 24.01.2017 в 09:25
0

First, an empty element must be added to the spinner's options to represent that nothing has been selected.

Frutas = ["", "manzana", "banana", ...];
Verduras = ["", "zanahoria", "pepino", ...];

And then onItemSelected should be:

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
    if(position != 0) { // la primer opcion es la vacia.
        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_main,new BP_Zanahorias()).commit();
    }
}
    
answered by 09.07.2017 в 02:29