How to make an Android picker from a fixed list of items?

3

I need a way to select cardinal points by something like a picker.

The objective is to indicate the meaning of an event that may be "NORTH-SOUTH", "EAST-WEST" ... and the opposites

    
asked by Leandro El Colo Noir 31.03.2016 в 00:10
source

2 answers

2

Since it is an immutable collection, you can list the elements in your strings.xml

<string-array name="puntos_cardinales">
<item>NORTE -> SUR</item>
<item>ESTE -> OESTE</item>
<item>TIERRA -> MARTE</item>
</string-array>

And in the layout add

<Spinner 
        android:id="@+id/mi_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/puntos_cardinales"
    />

If you want it to open as a popup, use android:spinnerMode="dialog" .

Then to get the selected value from the Activity, you do this:

Spinner puntosCardinales =(Spinner) findViewById(R.id.mi_spinner);
int indice_del_elemento = puntosCardinales.getSelectedItemPosition();
String texto_del_elemento = puntosCardinales.getSelectedItem().toString();

If you need to re-establish it (for example in the onResume of the activity)

Spinner puntosCardinales =(Spinner) findViewById(R.id.mi_spinner);
puntosCardinales.setSelection(indice_del_elemento);
    
answered by 31.03.2016 / 00:28
source
1

You can create a DialogFragment

Defining the title and values in strings.xml:

<resources>    
    ...
    ...
    <string name="pick_title">Coordenadas:</string>

    <string-array name="coordenadas">
        <item>NORTE - SUR</item>
        <item>ESTE - OESTE</item>
        <item>SUR - NORTE</item>
        <item>OESTE - ESTE</item>
    </string-array>
</resources>

We create a% custom DialogFragment :

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

public class CoordenadasDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.pick_title)
                .setItems(R.array.coordenadas, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity(),"Seleccionada: " +  getResources().getStringArray(R.array.coordenadas)[which] ,Toast.LENGTH_SHORT).show();
                    }
                });

        return builder.create();
    }
}

we just have to send the DialogFragment to show:

private void muestraPicker() {
    FragmentManager fm = getSupportFragmentManager();
    CoordenadasDialog miDialogo = new CoordenadasDialog();
    miDialogo.show(fm, "fragmento_coordenadas");
}

This would be the result:

Clicking an item would show you a Toast with the selected option.

The other option could be using a Spinner .

    
answered by 31.03.2016 в 20:21