Loop of random numbers

Asked

Viewed 102 times

0

Good afternoon, I’m trying to make a loop of my random numbers.... I have two spinner, one with a choice of how many random numbers and the other how many times it has to appear in a Listview...

my problem is that it generates the amount of random numbers, but not the amount of times. I wonder how to do .... 'Cause I’m not getting it

Code:

package com.nathan.lotogera.lotogera.Fragments;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.nathan.lotogera.lotogera.Controller.MegaSenaController;
import com.nathan.lotogera.lotogera.R;

import java.util.ArrayList;
import java.util.List;

public class DuplaSena extends Fragment {

    private TextView groupText, quantJogos;
    private Button calculate;
    private SeekBar jogos;
    private ListView lista;
    private Spinner spinner, teste;

    String [] valuesRound =
            {"Quantidade de Nº","6", "7"};

    String [] multiplicar =
            {"Teste","1","2", "3"};

    public DuplaSena() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_dupla_sena, container, false);

        groupText = (TextView) view.findViewById(R.id.txtResult);
        lista = (ListView) view.findViewById(R.id.listateste);
        spinner = (Spinner) view.findViewById(R.id.spinnerNumber);
        teste = (Spinner) view.findViewById(R.id.teste);
        calculate = (Button) view.findViewById(R.id.btnMostrar);

        groupText.setVisibility(View.INVISIBLE);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, valuesRound);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 1:
                        geraSix();
                        break;

                    case 2:
                        geraSeven();
                        break;
               }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(getActivity(), "Nenhum item selecionado!", Toast.LENGTH_LONG).show();
            }
        });


        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                groupText.setVisibility(View.VISIBLE);
            }
        });



        ArrayAdapter<String> adapters = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, multiplicar);
        adapters.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        teste.setAdapter(adapters);

        teste.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position){
                    case 1:
                        int qant = teste.getSelectedItemPosition();
                        for(int a = 0; a>=qant; a++){
                            geraSix();
                        }
                        break;

                    case 2:
                        int qz = teste.getSelectedItemPosition();
                        for(int a = 1; a<=qz; a++){
                            geraSix();
                        }
                        break;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(getActivity(), "Nenhum item selecionado!", Toast.LENGTH_LONG).show();
            }
        });

        return view;
    }


    public void geraSix(){
        List<String> teste = new ArrayList<>();

        final ArrayAdapter<String> listagem = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                teste
        );

        lista.setAdapter(listagem);

        MegaSenaController numberRandom = new MegaSenaController();
        numberRandom.megaSena();
        String groupFirst = numberRandom.getPrimary();
        listagem.add(groupFirst);
    }
}

inserir a descrição da imagem aqui

Can anyone help me? Thank you...

1 answer

1

I believe the Arraylist teste has to stay out of the method geraSix, for it not to be constantly initialized (and lose the previous values), that is, its Gerasix method only adds strings in this Arraylist and not in Adapter. When you need to reset the Arraylist, do it at the beginning of the loop that calls the Gerasix methods.

Listview Adapter you can initialize right at the beginning of Activity, even if empty (and not constantly in Gerasix). When the loop that calls the Gerasix methods ends, you simply call the method adapter.notifyDataSetChanged() that the spinner will be updated.

Something like that:

List<String> teste;
ArrayAdapter<String> listagem;

public View onCreateView(...) {
    ...
    teste = new ArrayList<>();
    listagem = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                teste
    );

    lista.setAdapter(listagem);
    ...
}

public void geraSix(){
        MegaSenaController numberRandom = new MegaSenaController();
        numberRandom.megaSena();
        String groupFirst = numberRandom.getPrimary();
        teste.add(groupFirst);
    }


// No código que for rodar os GeraSix
teste.clear();
for(...){
   GeraSix();
}
listagem.notifyDataSetChanged();
  • Good evening, I could not understand what you meant, if I take the Arraylist as the gerarSix will add in the Adapter? That’s what I didn’t understand

  • So, like I said, Gerasix only needs to add items in Arraylist. When you have finished running all the necessary Gerasix, call the notifyDataSetChanged() from the Adapter that it will re-read the Arraylist and update the spinner, assuming you have already initialized the Adapter there on Oncreate, as I also suggested.

  • I edited the answer

Browser other questions tagged

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