0

I started building an app that's supposed to display sales volume using a chart c/o achartengine with "some" success. Downloading the data took sometime and and the screen simply turns black but the graph gets displayed after a few seconds. It was the case until i learned about ASYNCTASK. After moving the layout.addview inside OnPostEexecute, the execution seemed better aligned with android rules concerning excessive time in loading the UI. But something peculiar happened, panning and zooming are not working anymore. I tried some of the recommendations here e.g. addPanListener & addZoomListener to no avail. The mrender.setZoomEnabled(true, true) , mrender.setPanEnabled(true, true) were also predefined. Would appreciate ideas how to resolve this. Thanks in advance.

Below is the code:

public class ActivityTREND extends Activity {

ProgressDialog pDialog;


Spinner tedit3;
AutoCompleteTextView tedit1, tedit2;

public static String selReg, selStore, selDays;

String dregion, dstore, ddays;
String defReg="%";
String defStore="%";
int defDays=14;

String mode;

GraphicalView graphicalView1,graphicalView2;
GraphTrend pie1, bar2;

LinearLayout layout1,layout2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_trend);

    // ALLOW VIOLATION OF THREAD POLICY
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    // FIX ORIENTATION TO PORTRAIT MODE
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);



    tedit1 = (AutoCompleteTextView) findViewById(R.id.trend_edit1);
    tedit2 = (AutoCompleteTextView) findViewById(R.id.trend_edit2);
    tedit3 = (Spinner) findViewById(R.id.trend_edit3);

    //AUTO-COMPLETE REGION
    String[] autoRegion = getResources().getStringArray(R.array.reggrp);

    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoRegion);

    tedit1.setAdapter(adapter);


    // AUTO-COMPLETE STORE
    String[] autoStore = getResources().getStringArray(R.array.stores);

    ArrayAdapter<String> adapter2 =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, autoStore);
    tedit2.setAdapter(adapter2);




    ArrayAdapter<CharSequence> adapter3 =  ArrayAdapter.createFromResource(this, R.array.days, android.R.layout.simple_spinner_dropdown_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    tedit3.setAdapter(adapter3);

    layout1 = (LinearLayout) findViewById(R.id.trend_graph1);
    layout2 = (LinearLayout) findViewById(R.id.trend_graph2);



// SHOW VOLUME GRAPHICALLY USING DECLARED CONSTANTS
    showVolume(null);

}



public void showVolume(View view){


    new Mytask().execute();


}


public void getVolume(View view) {


    dregion = "" + tedit1.getText().toString();
    if (dregion == null) {
        //String dstore = "" + tedit2.getSelectedItem().toString();
        dregion = "" + defReg;
        dstore = "" + defStore;
        ddays = "" + defDays;
    }else{
        dregion = "" + tedit1.getText().toString();
        dstore = "" + tedit2.getText().toString();
        ddays = "" + tedit3.getSelectedItem().toString();

    }

    selReg = "" + dregion;
    selStore = "" + dstore;
    selDays = "" + ddays;




    // PIE GRAPH
    //GraphTrend pie1 = new GraphTrend();
    pie1 = new GraphTrend();
    graphicalView1 = pie1.getPERprodgrpPIE(this);

    //layout1 = (LinearLayout) findViewById(R.id.trend_graph1);
    //layout1.removeAllViews();
    //layout1.addView(graphicalView1);


    // BAR GRAPH
    //GraphTrend bar2 = new GraphTrend();
    bar2 = new GraphTrend();
    //GraphicalView graphicalView2 = bar2.getPERprod(this);
    graphicalView2 = bar2.getPERprod(this);

    //layout2 = (LinearLayout) findViewById(R.id.trend_graph2);
    //layout2.removeAllViews();
    //layout2.addView(graphicalView2);



}





class Mytask extends AsyncTask<String,Void,Void> {

    @Override
    protected void onPreExecute() {


        // Showing progress dialog before making http request
        pDialog = new ProgressDialog(ActivityTREND.this);
        pDialog.setMessage("Downloading...");
        pDialog.show();


    }

    @Override
    public Void doInBackground(String... params) {


        Looper.prepare();

        getVolume(null);

        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    public void onPostExecute(Void aVoid) {


        layout1.removeAllViews();
        layout1.addView(graphicalView1);

        layout2.removeAllViews();
        layout2.addView(graphicalView2);


        pDialog.hide();
    }
}

)

userMMS
  • 1
  • 2
  • Can you post your code? –  Aug 21 '15 at 06:20
  • I tried adding the touching code for ASYNCTASK at the OnCreate() before showVolume() to ensure it's running on the same thread but met no success here. If I restore the old code without the ASYNCTASK, interaction with the graphs are possible. – userMMS Aug 21 '15 at 12:41
  • I think I figured this one out after hours of testing. The solution is to separate the graphing method from the httpget done inside the doInBackground and transfer to OnPostExecute to ensure the graphs are part of the main UI thread. – userMMS Aug 22 '15 at 05:01

0 Answers0