How to place a method value inside a Listview

Asked

Viewed 64 times

0

Good evening, I’m creating an app that generates random numbers for me, only I had an idea that makes it easier for the user.... When he wants to generate more than one block of random numbers, he chooses the amount he wants and has it generated, and shows the blocks of numbers generated according to what the user has chosen....

With gambiarra you can do: create several textview and adapt with the switch... but wanted to do something right...

I created a method generates what instance the class with the function of generating random numbers, and within the seekbar put a switch to do this action according to the amount the user chose... but wanted to show inside a listview. Is there a way to do that? Can someone help me?

code:

package com.nathan.lotogera.lotogera.Fragments;


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

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

import java.util.ArrayList;
import java.util.Random;

public class DuplaSena extends Fragment {

    private TextView group1, alert, group2, quantJogos;
    public Button calculate;
    public SeekBar jogos;
    private ListView lista;

    public DuplaSena() {
        // Required empty public constructor
    }


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

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

        group1 = (TextView) view.findViewById(R.id.textView1);
        alert = (TextView) view.findViewById(R.id.textView2);
        group2 = (TextView) view.findViewById(R.id.textView3);
        quantJogos = (TextView) view.findViewById(R.id.textjogoShow);
        jogos = (SeekBar) view.findViewById(R.id.seekJogos);
        lista = (ListView) view.findViewById(R.id.listateste);

        calculate = (Button) view.findViewById(R.id.button1);

        String[] teste = new String[0];

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

        lista.setAdapter(listagem);

        jogos.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                switch (progress){
                    case 1:
                        gera();

                        break;

                    case 2:
                        gera();

                        break;
                }

                quantJogos.setText("reste " + progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(getActivity(), "parou", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    public void gera(){
        NumberRandom numberRandom = new NumberRandom();

        numberRandom.megaSena();

        String groupFirst = numberRandom.getPrimary();
        String groupSecond = numberRandom.getSecond();
    }
}

1 answer

2


I created your test vector as List<String> teste = new ArrayList<String>();. Try to create this vector as a class variable and not local, so it can be called in the method generates().

List<String> teste = new ArrayList<String>();

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

Browser other questions tagged

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