How to create a String Array from a String Array and an Integer Arraylist?

Asked

Viewed 199 times

0

I am creating an application that will need to store the specialties of each clinic registered in the application, these specialties later I will be in the database of Firebase, I am using a dialog with checkboxes where will show the specialties of the clinic, I am also using an array of strings that has a reference to an array defined in the.xml string with the specialties, and an Arraylist that saves the results, which items from the list of specialties have been selected, but I cannot convert the selected items into a list with the name of the specialties that were selected.

In the setPositiveButton method, there is an example that I can pass the specialties only to a common string.

Dialog com as especialidades

private boolean[] especialidadesChecadas;
private String[] listaEspecialidades;
private ArrayList<Integer> mEspecialidadeSelecionados = new ArrayList<>();


    botaoEspecialidades.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(redeSelecionada != null){

                AlertDialog.Builder mBuilder = new AlertDialog.Builder(CadastroActivity.this);
                mBuilder.setTitle("Seleciona as especialidades:")
                .setMultiChoiceItems(listaEspecialidades, especialidadesChecadas, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int position, boolean isChecked) {

                        if(isChecked){
                            if(!mEspecialidadeSelecionados.contains(position)){
                                mEspecialidadeSelecionados.add(position);
                            }
                        }   else if (mEspecialidadeSelecionados.contains(position)){
                                mEspecialidadeSelecionados.remove((Integer) position);
                                    }
                    }
                });

                mBuilder.setCancelable(false);
                mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String item = "";

                        for(int i= 0; i < mEspecialidadeSelecionados.size(); i++){
                            item = item + listaEspecialidades[mEspecialidadeSelecionados.get(i)];

                            if(i != mEspecialidadeSelecionados.size() -1){
                                item = item + ", ";
                            }
                        }

                        //DESSA FORMA CONSIGO VER TODAS AS ESPECIALIDADES NUMA UNICA STRING, MAS PRECISAVA 
                        //DE UM ARRAY DE STRING PARA ARMAZENAR DO FIREBASE DATABASE PARA PUXAR POSTERIORMENTE
                        Toast.makeText(CadastroActivity.this, ""+item, Toast.LENGTH_SHORT).show();

                    }
                });

                mBuilder.setNegativeButton("Fechar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                    }
                });

                mBuilder.setNeutralButton("Limpar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //Limpar selecionados
                        //Passa por todos os itens e deseleciona
                        for(int i = 0; i < especialidadesChecadas.length; i++){
                            especialidadesChecadas[i] = false;
                            mEspecialidadeSelecionados.clear();

                        }

                    }
                });


                AlertDialog mDialog = mBuilder.create();
                mDialog.show();
  • If you need an array of strings, just create the array, something like List<String> strings = new ArrayList<>(); and make strings.add(item) instead of the item = item + ", ";. If you then need to build a string based on the array joining the various elements, just use String.join

  • I did the test, but when sending to the Firebase Database it creates only one position and puts all the items there, example: [0]: Immunology, Orthodontics, does not separate them into other positions. If I store this way I don’t think I’ll be able to consult for specialties afterwards, since everyone will be in the same position, right?

  • Add to a ArrayList with the method add puts each item in a new position. Now how it looks in Firebase depends on how you are updating the information there

  • Thanks, I got through a for that goes through all the items and add in List<String> specialties.

No answers

Browser other questions tagged

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