这是我的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);
}
}
在logcat中,它说400 frames skipped: application doing too much work in Main Thread
此外,有时候我的listview没有显示任何数据。但是当我点击搜索操作时,列表视图会显示数据
我要做的下一步是searchButtons布局将替换为搜索类型的片段。主线程上的这个工作会不会太多?
答案 0 :(得分:1)
解决这个问题需要识别可能发生或可能发生长时间处理的节点。最好的方法是进行所有处理,无论与主UI线程分开的线程有多小或多大。因此,无论是从SQLite数据库访问数据还是进行一些核心数学运算,或者只是对数组进行排序 - 在不同的线程中进行。