Reputation: 1479
Here is my MainActivity.class
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private Toolbar toolbar;
private static final String URL = "http://192.168.0.100:8888/androidrest/rest/service/getStores";
private ProgressDialog progressDialog;
private List<Store> stores = new ArrayList<Store>();
private static final int ACTION_SEARCH = Menu.FIRST;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
}
new StoreDataLoader().execute();
}
private class StoreDataLoader extends AsyncTask<String, Void, List<Store>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading Stores....");
progressDialog.show();
}
@Override
protected List<Store> doInBackground(String... params) {
JsonArrayRequest request = new JsonArrayRequest(URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hideProgressDialog();
for (int i = 0; i < response.length(); i++) {
try {
if (BuildConfig.DEBUG) {
Log.d("MainActivity", i + "");
}
JSONObject jsonObj = response.getJSONObject(i);
Store s = new Store();
s.setAddress(jsonObj.getString("address"));
s.setDesc(jsonObj.getString("desc"));
s.setName(jsonObj.getString("name"));
s.setStoreImg(jsonObj.getString("storeImage"));
s.setDistance(jsonObj.getString("dist"));
stores.add(s);
} catch (JSONException exp) {
exp.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "ERROR: " + error.getMessage());
hideProgressDialog();
}
});
HowmuchCacheCtrl.getInstance().addToRequestQueue(request);
if (BuildConfig.DEBUG) {
if (stores != null) {
Log.d("MainActivity", "doInBackground " + stores.size());
}
}
return stores;
}
@Override
protected void onPostExecute(List<Store> stores) {
super.onPostExecute(stores);
ListView storeListView = (ListView) findViewById(R.id.storeList);
StoreListAdapter listAdapter = new StoreListAdapter(MainActivity.this, stores);
storeListView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
if (BuildConfig.DEBUG) {
Log.d("MainActivity", "onPostExecute " + stores.size());
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
hideProgressDialog();
}
private void hideProgressDialog() {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
LinearLayout searchBtns = (LinearLayout) findViewById(R.id.searchButtons);
searchBtns.setVisibility(View.VISIBLE);
break;
}
return super.onOptionsItemSelected(item);
}
}
In logcat, it says, 400 frames skipped: application doing too much work in Main Thread
Also, sometimes my listview is not showing up any data. But when I click on search action, list view comes up with data
The next step I want to do is searchButtons layout will be replaced with fragments on type of search. Will this be too much work on Main Thread?
Upvotes: 0
Views: 171
Reputation: 3536
Fixing this requires identifying nodes where there is or possibly can happen long duration of processing. The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. So be it accessing data form SQLite Database or doing some hardcore maths or simply sorting an array – Do it in a different thread.
Upvotes: 1