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();
}
}
)