Asynctask onPreExecute method problem - Android Studio

Asked

Viewed 291 times

0

I have this Activity that displays the user score of my application. However, when this Activity is called, what should occur is a ProgressDialog being displayed with the message "Buscando Pizza" (according to the onPreExecute) and when the chart is mounted (with the recovered information and tals), the Progress dialog should disappear and the user should see the chart.

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 recovered in the Background and on onPostExecute the Pie Chart setup itself. but the Chart pie data is giving null = is having some problem in the doInBackground because it is not running properly and is not recovering the values of the variables:

dotCor, dotNumber, dotObject

  • You are fast because your Asynctask just arrow a Systener and ends, ie the processing of the graph itself is in another thread.

  • Dude, the main goal of Progressdialog is not aesthetics, it is precisely so that the user realizes that something is happening so that he does not wait by hand.

  • @Márciooliveira I made changes to my code and updated the post. look at the question again, please.

  • @acklay Yes! exactly. By default, the android Piechart displays an English message when there is no data entry in the chart. I wanted to supply this message with a Progressdialog that will wait to receive the data to mount the Piechart.

  • As I said, your Asynctask initializes a system that will run on another thread. There are no guarantees (most likely not) that this Liener will "hear" something before the asynctask ends, so he must be returning the "dot" object as null, as it ends before the onDataChanged writes something in the "dot". My question: What do you need this Asynctask for? Why not set this System in the Activity Oncreate where you initialize Asynctask, take what’s in Onpostexecute and play into Ondatachanged, and delete this Asynctask?

1 answer

0

I believe you are using an unnecessary Asynctask, as all it does is initialize a Listener that has no guarantee that it will return any value before the Asynctask completes.

Why don’t you try a change: I believe your Activity should have the command that initializes your Asynctask, like this:

new AtualizarPie<>().execute();

Delete this line and put the code below (or put it into 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();
        }
    });

Browser other questions tagged

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