我一直在研究活动生命周期以及它们如何释放可用空间以便我的应用程序可以更顺畅地运行。只是我想将这些方法用于我的一项活动,因为当我的 PDialog 启动时,我的数据需要相当长的时间才能加载。我一直在查看我的代码,看看我可以在哪里放置这些方法,所以当我的活动正在执行我的 asynctask 时,那将是我想要在那个时间点运行的唯一事情。我下面的代码将清除这些事情。
public class ListView extends ListActivity {
ArrayList<HashMap<String, String>> questionList;
final String TAG_RESULTS = "results";
final String TAG_QUESTION_SUBJECT = "Subject";
final String TAG_QUESTION_NUMANSWERS = "NumAnswers";
final String TAG_QUESTION = "question";
final String TAG_QUESTION_CONTENT = "Content";
final String TAG_QUESTION_CHOSENANSWER = "ChosenAnswer";
final String TAG_ANSWERS = "Answers";
final String TAG_ANSWER = "Answer";
final String TAG_ANSWERS_CONTENT = "content";
final String TAG_QUERY = "query";
JSONArray question = null;
android.widget.ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.listview);
questionList = new ArrayList<HashMap<String, String>>();
new LoadAllData().execute();
}
@Override
protected void onListItemClick(android.widget.ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
HashMap<String, String> item = questionList.get(pos);
Intent i = new Intent(ListView.this, SingleListItem.class);
i.putExtra(TAG_QUESTION_SUBJECT, item.get(TAG_QUESTION_SUBJECT));
i.putExtra(TAG_QUESTION_CONTENT, item.get(TAG_QUESTION_CONTENT));
i.putExtra(TAG_QUESTION_CHOSENANSWER, item.get(TAG_QUESTION_CHOSENANSWER));
startActivity(i);
}
class LoadAllData extends AsyncTask<String, String, String> {
private Dialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pDialog;
pDialog = new ProgressDialog(ListView.this);
pDialog.setMessage("Loading Data. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
try {
Intent in = getIntent();
String searchTerm = in.getStringExtra("TAG_SEARCH");
String query = URLEncoder.encode(searchTerm, "utf-8");
String URL = "http://example.com";
JSONParsser jParser = new JSONParsser();
JSONObject json = jParser.readJSONFeed(URL);
try {
JSONArray questions = json.getJSONObject("all").getJSONArray("questions");
for(int i = 0; i < questions.length(); i++) {
JSONObject question = questions.getJSONObject(i);
String Subject = question.getString(TAG_QUESTION_SUBJECT);
String NumAnswers = question.getString(TAG_QUESTION_NUMANSWERS);
String ChosenAnswer = question.getString(TAG_QUESTION_CHOSENANSWER);
String Content = question.getString(TAG_QUESTION_CONTENT);
//JSONArray Answers = question.getJSONObject(TAG_ANSWERS).getJSONArray(TAG_ANSWER);
//JSONObject Answer = Answers.getJSONObject(0);
//String Content = Answer.getString(TAG_ANSWERS_CONTENT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_QUESTION_SUBJECT, Subject);
map.put(TAG_QUESTION_NUMANSWERS, NumAnswers);
map.put(TAG_QUESTION_CONTENT, Content);
map.put(TAG_QUESTION_CHOSENANSWER, ChosenAnswer);
questionList.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TAG_QUESTION ;
}
@Override
protected void onPostExecute(String file_URL) {
if (pDialog != null && pDialog.isShowing()) pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(getBaseContext(), questionList,
R.layout.listelements,
new String[] { TAG_QUESTION_SUBJECT, TAG_QUESTION_NUMANSWERS }, new int[] {
R.id.Subject, R.id.NumAnswers, });
setListAdapter(adapter);
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
在我上一堂课中,我已经onStop
释放了一些空间。但我希望现在你能明白我的意思,当我说我只想让 PDialog 和我的 asynctask 在那个时间点运行,这样我的 PDialog 不会减慢进程。