Asynctask不会更新数组

时间:2013-06-07 01:05:45

标签: android

我在从类文件asynctask访问此A填充的arraylists时遇到问题。从类文件B我尝试使用A等方法,如

inStream = new InboxLoader(MainInbox.this,1); Log.e("JJJ",""+inStream.getMemId().size());

来自不同类文件的

方法我在logcat中遇到崩溃说indexoutofbounds error 我正在寻找替代方法,将AsyncTask放在与班级B相同的文件中     公共类InboxLoader {     上下文ctx;     ArrayListmemName,memAvatar,msgBody,MSGTIME;     ArrayList msgId,memId;

InboxLoader(Context context,int id){
    this.ctx = context;
    this.msgBody   = new ArrayList<String>();
    this.memAvatar = new ArrayList<String>();
    this.memId     = new ArrayList<Integer>();
    this.memName   = new ArrayList<String>();
    this.msgTime   = new ArrayList<String>();
    this.msgId     = new ArrayList<Integer>();


    new LoadStream().execute(id);

}


public ArrayList<String> getMsgBody(){
    return msgBody;
}

public ArrayList<String> getMemAvatar(){
    return memAvatar;
}

public ArrayList<Integer> getMemId(){
    return memId;
}

public ArrayList<String> getMemName(){
    return memName;
}

public ArrayList<Integer> getMsgId(){
    return msgId;
}

public ArrayList<String> getMsgTime(){
    return msgTime;
}

/*
 * STARTS GRABBING DATA FOR THE LISTVIEW
     */

public class LoadStream extends AsyncTask<Integer, Integer, JSONObject> {
    ProgressDialog progressBar;

    @Override
    protected void onPreExecute() {
            progressBar = new ProgressDialog(ctx, ProgressDialog.STYLE_SPINNER);
            progressBar.setMessage("Generating Inbox....");
            progressBar.show();

        super.onPreExecute();
    }

    @Override
    protected JSONObject doInBackground(Integer... params) {
        List<NameValuePair> params2 = new ArrayList<NameValuePair>();
        params2.add(new BasicNameValuePair("memberId", String.valueOf(1)));
        params2.add(new BasicNameValuePair("type", "get"));
        JSONObject json = new jsonParser().makeRequest("url", params2);
        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        super.onPostExecute(json);

        int success = 0;
        JSONObject jData;
        JSONArray jObj = null;

        try{
            // successfully received details
            jObj = json.getJSONArray("details"); // JSON Array
            success = json.getInt("success");
        }catch(Exception e){
            e.printStackTrace();
        }

        if(success == 1){

            try{
                for( int i = 0; i < jObj.length(); i++ ){
                    //GET OBJECT FROM JSON Array
                    jData = jObj.getJSONObject(i);
                    //ASSIGN VALUES
                    msgId.add(jData.getInt("msg_id"));
                    memAvatar.add(jData.getString("mem_avatar"));
                    memName.add(jData.getString("mem_name"));
                    memId.add(jData.getInt("mem_id"));
                    msgBody.add(jData.getString("msg_body"));
                    msgTime.add(jData.getString("msg_time"));
                }
            }catch(Exception e){
                Log.e("STREAM FILE PROBLEM", e.getMessage());
            }

        }
            progressBar.dismiss();
            Log.e("STREAM FILE PROBLEM",""+memId.size());//prints 1 
    }

}
 }

1 个答案:

答案 0 :(得分:1)

您正在尝试在启动AsyncTask后立即读取ArrayList,而在doInBackground结束之前结果将无法使用。
您应该在onPostExecute中执行您想要的操作,以确保任务正确执行 的编辑:
定义一个如下所示的监听器:

    public interface RequestListener {
        public void onComplete(T response);
    }

并在InboxLoader中添加一个字段requestListener:

InboxLoader(Context context,int id, RequestListener li){
    this.ctx = context;
    this.msgBody   = new ArrayList<String>();
    this.memAvatar = new ArrayList<String>();
    this.memId     = new ArrayList<Integer>();
    this.memName   = new ArrayList<String>();
    this.msgTime   = new ArrayList<String>();
    this.msgId     = new ArrayList<Integer>();
    this.requestListener     = li;

    new LoadStream().execute(id);

}
onPostExecute中的

添加:

protected void onPostExecute(JSONObject json) {
        super.onPostExecute(json);

        int success = 0;
        JSONObject jData;
        JSONArray jObj = null;

        try{
            // successfully received details
            jObj = json.getJSONArray("details"); // JSON Array
            success = json.getInt("success");
            if(requestListener!=null)
                requestListener.onComplete();
        }catch(Exception e){
            e.printStackTrace();
        }

然后你可以在你的B类中的onComplete定义中处理结果:

    inStream = new InboxLoader(MainInbox.this,1, new RequestListener() {
        public void onComplete(){
            Log.e("JJJ",""+inStream.getMemId().size());
        };
    });