Loop of random numbers

0

Good afternoon, I'm trying to loop my random numbers .... I have two spinner, 1 with a choice of how many random numbers and the other how many times have to appear in a ListView ...

My problem is that it generates the amount of random numbers, but not the amount of times. I wanted to know how to do it .... Well I can not do 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);
    }
}

Can anyone help me? Thank you ...

    
asked by anonymous 29.07.2017 / 21:44

1 answer

1

I think ArrayList teste has to be outside the geraSix method, so it is not constantly initialized (and it loses previous values), that is, its GeraSix method only adds strings to this ArrayList and not to the adapter. When you need to zero the ArrayList, do at the beginning of the loop that calls the GeraSix methods.

The ListView adapter you can boot to at the beginning of the Activity, even if empty (not constantly in GeraSix). When the loop that calls the GeraSix methods ends, you simply call the adapter.notifyDataSetChanged() method that the spinner will be updated.

Something like this:

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();
    
30.07.2017 / 16:37