Spinner does not load data from the.xml string

Asked

Viewed 193 times

2

My spinner only appears the arrow. no 1 has to show the States and so informed 2 shows the cities of that state inserir a descrição da imagem aqui

This is the java code.

public class PropertyRegisterActivity extends AppCompatActivity implements OnItemSelectedListener{


private Spinner spProvincias;
private Spinner spLocalidades;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_property_register);

    this.spProvincias = (Spinner) findViewById(R.id.sp_provincia);
    this.spLocalidades = (Spinner) findViewById(R.id.sp_localidad);

    loadSpinnerProvincias();

}

private void loadSpinnerProvincias() {


    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.provincias, R.layout.spinner_item);

    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

    this.spProvincias.setAdapter(adapter);


    this.spProvincias.setOnItemSelectedListener( this);
    this.spLocalidades.setOnItemSelectedListener(this);

}


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
                               long id) {

        switch (parent.getId()) {
            case R.id.sp_provincia:


                TypedArray arrayLocalidades = getResources().obtainTypedArray(
                        R.array.array_provincia_a_localidades);
                CharSequence[] localidades = arrayLocalidades.getTextArray(pos);
                arrayLocalidades.recycle();



                ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                        this, R.layout.spinner_item,
                        android.R.id.text1, localidades);


                adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);


                this.spLocalidades.setAdapter(adapter);

                break;

            case R.id.sp_localidad:

                break;
        }
    }

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


    }
}

That and xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="view.PropertyRegisterActivity">

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="360dp"
        android:layout_height="82dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:contentDescription="TODO"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/ic_cadastrodepropiedade_logo"
        tools:ignore="VectorDrawableCompat" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="23dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/property_register_description"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView5"
        app:layout_constraintVertical_bias="0.096" />

    <EditText
        android:id="@+id/editYear"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="@string/hint_year"
        android:inputType="numberDecimal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView5"
        app:layout_constraintVertical_bias="0.309" />

    <EditText
        android:id="@+id/editProperty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="@string/hint_property"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView5"
        tools:ignore="MissingConstraints,UnknownId" />

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/custom_button"
        android:text="@string/save"
        android:textColor="@color/text_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UnknownId" />


    <Spinner
        android:id="@+id/sp_provincia"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:spinnerMode="dropdown"
        android:prompt="@string/provincia_prompt"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView5"
        app:layout_constraintVertical_bias="0.648" />


    <Spinner
        android:id="@+id/sp_localidad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:spinnerMode="dropdown"
        android:prompt="@string/localidad_prompt"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView5"
        app:layout_constraintVertical_bias="0.805" />




</android.support.constraint.ConstraintLayout>

These are the layouts for the spinner.

spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#ff0000" />

spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#aa66cc"/>

1 answer

0

Try switching from prompt to Entries

android:entries="@array/tiposPessoa"

in the strings.xml put the data

    <string-array name="tiposPessoa">
     <item>Selecione uma opção</item>
     <item>Usuário</item>
     <item>Gestor</item>
     <item>Usuário/Gestor</item>
    </string-array>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.