1

asynctask我在从类文件访问由此填充的数组列表时遇到问题A。从类文件B中,我尝试使用 A 一种方法,例如

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

来自不同类文件的方法我在 logcat 中遇到崩溃,说indexoutofbounds error 我正在寻找一种替代方法,以将其与 class public class InboxLoader { Context ctx;AsyncTask放在同一个文件中 BArrayListmemName,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 
    }

}
 }
4

1 回答 1

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());
        };
    });
于 2013-06-07T01:28:32.720 回答