gpt4 book ai didi

android - Asynctask 不会更新数组

转载 作者:行者123 更新时间:2023-11-30 03:33:45 25 4
gpt4 key购买 nike

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

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

来自不同类文件的方法 我在 logcat 中遇到崩溃,提示 indexoutofbounds error我正在寻找一种替代方法,以将此 AsyncTask 与类 B 放在同一文件中 公共(public)类 Inb​​oxLoader { 语境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
}

}
}

最佳答案

您正在尝试在启动 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());
};
});

关于android - Asynctask 不会更新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16974702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com