我的异步任务不工作。对话框不会关闭并且列表没有被更新(我认为是因为 onPostExecute 没有被调用)。我用谷歌搜索了它,但没有结果。
package com.example.whs;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Albums extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// Array for the list
ArrayList<HashMap<String, String>> itemList;
// url to get the items
private static String url_all_products = "<my url here>";
// JSON variables
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_ITEMS = "items";
private static final String TAG_NAME = "name";
private static final String TAG_ID = "id";
private static final String TAG_USERNAME = "username";
//JSON array
JSONArray items = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_albums);
// HashMap for the items
itemList = new ArrayList<HashMap<String, String>>();
// Loading the items on the background
new LoadAllItems().execute();
// Get list-view
ListView lv = getListView();
// On item click
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//do
}
});
}
// class for loading all items
class LoadAllItems extends AsyncTask<String, String, String> {
// Before
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(Albums.this);
pDialog.setMessage("Albums laden...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
// Get the products
@Override
protected String doInBackground(String... params) {
// Build Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
//Get the JSON string
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params1);
// Check for response
Log.d("All Items: ", json.toString());
try {
// check for success tag
int success = json.getInt(TAG_SUCCESS);
if(success == 1) {
// found the items
items = json.getJSONArray(TAG_ITEMS);
// loop through the items
for (int i = 0; items.length() > i; i++){
// Get the item in variable c
JSONObject c = items.getJSONObject(i);
// Store in a variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String username = c.getString(TAG_USERNAME);
if(username == ""){
username = "onbekend";
}
// Create the HashMap
HashMap<String, String> map = new HashMap<String, String>();
// add it
map.put(TAG_ID, id);
map.put(TAG_USERNAME, username);
map.put(TAG_NAME, name);
// to arraylist
itemList.add(map);
}
}else{
// nothing found
Intent i = new Intent(getApplicationContext(), Index.class);
// close the previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e){
e.printStackTrace();
}
return null;
}
}
protected void onPostExecute(String result){
// dismiss dialog
pDialog.dismiss();
runOnUiThread(new Runnable(){
public void run(){
// Add adapter to the list
MenuAdapter adapter = new MenuAdapter(Albums.this, itemList);
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.albums, menu);
return true;
}
}
循环正常工作。
如何解决这个问题?