problem with onPreExecute method of AsyncTask - Android Studio

0

I have this activity that displays the user's score of my application. However, when this activity is called, what should occur is a ProgressDialog being displayed with the message "Searching for Pizza" (according to onPreExecute) and when the graph is set up (with the information retrieved and tals), the progress dialog should disappear and the user should see the graph.

public class AtualizarPie extends AsyncTask<Object, Object, ArrayList<String>> {

    @Override
    protected void onPreExecute() {
        progressDialog.setMessage("Buscando Pizza...");
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected ArrayList<String> doInBackground(Object... params) {
        userDB.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();

                Log.v("mapString", map+"");

                pontuacaoObjeto = map.get("pontuacaoObjeto");
                pontuacaoCor = map.get("pontuacaoCor");
                pontuacaoNumero = map.get("pontuacaoNumero");

                pontos.add(pontuacaoCor);
                pontos.add(pontuacaoNumero);
                pontos.add(pontuacaoObjeto);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return pontos;
    }

    @Override
    protected void onPostExecute(ArrayList<String> s) {
        entries = new ArrayList<>();
        entries.add(new PieEntry(Integer.parseInt(pontos.get(0)), "cor"));
        entries.add(new PieEntry(Integer.parseInt(pontos.get(1)), "numero"));
        entries.add(new PieEntry(Integer.parseInt(pontos.get(2)), "objeto"));

        PieDataSet dataset = new PieDataSet(entries, "pontuações");

        ArrayList<Integer> colors = new ArrayList<Integer>();

        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());

        dataset.setColors(colors);
        dataset.setValueTextSize(30);

        PieData data = new PieData(dataset);

        PieChart chart = new PieChart(getApplicationContext());
        setContentView(chart);
        chart.setData(data);
        chart.setEntryLabelColor(R.color.colorPrimaryDark);
        chart.setEntryLabelTextSize(15);
        chart.setEntryLabelTypeface(Typeface.SANS_SERIF);
        chart.setContentDescription("pontuações");
        chart.setCenterText("Pontuações");
        chart.setCenterTextSize(15);

        progressDialog.hide();
    }
}

I put the chart data to be retrieved in the Background and onPostExecute the Pie Chart assembly itself. but the pie chart data is giving null = \ is having some problem in doInBackground because it is not running correctly and is not retrieving the values of the variables:

  

punctuationCor, punctuationNumber, punctuationObject

    
asked by anonymous 09.06.2017 / 16:40

1 answer

0

I believe you're using an unneeded AsyncTask because all it does is initialize a listener that has no guarantee whatsoever that will return some value before AsyncTask finishes.

Why do not you try a change: I believe that in your Activity you should have the command that initializes your AsyncTask, like this:

new AtualizarPie<>().execute();

Delete this line and put the code below (or put it inside a new method and call this method), then delete AsyncTask:

progressDialog.setMessage("Buscando Pizza...");
progressDialog.show();
userDB.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();

            Log.v("mapString", map+"");

            pontuacaoObjeto = map.get("pontuacaoObjeto");
            pontuacaoCor = map.get("pontuacaoCor");
            pontuacaoNumero = map.get("pontuacaoNumero");

            pontos.add(pontuacaoCor);
            pontos.add(pontuacaoNumero);
            pontos.add(pontuacaoObjeto);

            entries = new ArrayList<>();
            entries.add(new PieEntry(Integer.parseInt(pontos.get(0)), "cor"));
            entries.add(new PieEntry(Integer.parseInt(pontos.get(1)), "numero"));
            entries.add(new PieEntry(Integer.parseInt(pontos.get(2)), "objeto"));

            PieDataSet dataset = new PieDataSet(entries, "pontuações");

            ArrayList<Integer> colors = new ArrayList<Integer>();

            for (int c : ColorTemplate.VORDIPLOM_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.JOYFUL_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.COLORFUL_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.LIBERTY_COLORS)
                colors.add(c);

            for (int c : ColorTemplate.PASTEL_COLORS)
                colors.add(c);

            colors.add(ColorTemplate.getHoloBlue());

            dataset.setColors(colors);
            dataset.setValueTextSize(30);

            PieData data = new PieData(dataset);

            PieChart chart = new PieChart(getApplicationContext());
            setContentView(chart);
            chart.setData(data);
            chart.setEntryLabelColor(R.color.colorPrimaryDark);
            chart.setEntryLabelTextSize(15);
            chart.setEntryLabelTypeface(Typeface.SANS_SERIF);
            chart.setContentDescription("pontuações");
            chart.setCenterText("Pontuações");
            chart.setCenterTextSize(15);

            progressDialog.dismiss();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
    
09.06.2017 / 18:43