How do I format string in an android app?

Asked

Viewed 62 times

-1

When changing option it does not format the string values as shown in the image below.

inserir a descrição da imagem aqui

Please, how could I fix this?

This is my code.

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

public class RadioDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new AlertDialog.Builder(getActivity())
                .setTitle("Escolha um dos oito remédios naturais:")
                .setSingleChoiceItems(R.array.remedios, 6, this)
                .create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        String[] remedios = getActivity().getResources().getStringArray(R.array.remedios);
        String remedio = remedios[which];

        Toast.makeText(getActivity(), "Você escolheu  " + remedios, Toast.LENGTH_SHORT).show();
    }
}

This is my Resource file where the list is.

<resources>
    <string name="app_name">Diaglos</string>

    <string name="txt_dialog">Abrir Dialog</string>
    <string name="txt_dialog_simple">um simples dialog</string>
    <string name="txt_dialog_radio">Dialog de Radio Button</string>

    <string-array name="remedios">
        <item>Ar puro</item>
        <item>Água potável</item>
        <item>Luz Solar</item>
        <item>Alimentação Saudável</item>
        <item>Prática de Atividade física</item>
        <item>Domínio Próprio</item>
        <item>Confiança em Deus</item>
    </string-array>

</resources>

inserir a descrição da imagem aqui

  • 1

    On the Oast there, you shouldn’t be passing only medicine instead of medicine?

  • 1

    That was the problem, thank you very much, it was really lack of attention.

1 answer

0

The mistake was here

inserir a descrição da imagem aqui

The list value was placed instead of the variable representing the list position.

It’s the right thing to do!

inserir a descrição da imagem aqui

Browser other questions tagged

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