试图从asynctask填充listview

时间:2013-09-30 18:18:37

标签: android android-listview android-asynctask

尝试从异步任务填充列表视图...一切正常,我得到了我的json响应,构建了地图,但不知何故没有传递给onPostExecute并且在对话框被解除后listview保持为空...不要得到任何错误......任何人都有提示?

继承人代码:

import org.apache.http.impl.cookie.BasicClientCookie;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.view.Menu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONArray;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import com.loopj.android.http.*;

public class Success extends ListActivity {

private static final String TAG_TITLE = "title";
ProgressDialog mProgressDialog;
JSONArray data = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_success);
    new processHomeStream().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.success, menu);
    return true;
}

class processHomeStream extends AsyncTask<ArrayList<HashMap<String, String>>, Void, ArrayList<HashMap<String, String>>>{

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(Success.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    public ArrayList<HashMap<String, String>> doInBackground(ArrayList<HashMap<String, String>>... passing) {
        ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();

        final AsyncHttpClient client = new AsyncHttpClient();
        client.get("url-to-my-api", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) { 

        try {
            ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
            JSONObject json = new JSONObject(response);
            data = json.getJSONArray(TAG_ARRAY);
            for(int i = 0; i < data.length(); i++){
                // processing the son response
                }

                map.put(TAG_TITLE, title);
                result.add(map);                    
            }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
            }

        });

        return result;

    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result){
        super.onPostExecute(result);
        mProgressDialog.dismiss();
        System.out.println(result);
        final ListAdapter adapter = new SimpleAdapter(Success.this, result, R.layout.list_item,
                new String[] { TAG_TITLE},
                new int[] { R.id.title,});

        setListAdapter(adapter);
        }


    }

}

谢谢!

0 个答案:

没有答案