gpt4 book ai didi

java - Android线程通信AsyncTask

转载 作者:行者123 更新时间:2023-12-01 10:09:35 25 4
gpt4 key购买 nike

当前场景:示例应用程序将来自多个 URL 的图像存储在 SD 缓存中,并在 ListView 中显示它们。

任务:不要在 MainActivity 中的私有(private)方法中采用硬编码 URL,而是从 URL 资源中放置的 JSON 数据中检索它们。

我正在检索 JSON 并很好地解析数据,但我在如何将此解析的数据发送到 MyImageLoaderAdapter 方面遇到困难,因为返回的列表似乎稍后出现。

文件: MainActivity.java

public class MainActivity extends Activity {    
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
try{
SimpleAsyncTask mTask = new SimpleAsyncTask();
mTask.execute(resource);
ArrayList list = mTask.list;
String[] strArray = new String[ list.size() ];
int length = strArray.length; // lenght = 0
mStrings = new String[ list.size() ];
int length = strArray.length;
for( int j = 0; j < length; j++ ) {
mStrings[j] = list.get(j).toString();
}

}catch (Exception e){}

// Create custom adapter for listview
adapter=new MyImageLoadAdapter(this, mStrings);
...
}

private String[] mStrings={
"http://resourse1.com",
"http://resourseN.com",
};
}

文件: SimpleAsyncTask.java

public class SimpleAsyncTask extends AsyncTask<String, String, String>{
ArrayList list = new ArrayList();
protected String doInBackground(String... uri) {
//working code
}


@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
...
return list //expected value;
}
}

最佳答案

你可以这样做:

public class MainActivity extends Activity implements OnTaskCompleteListener{    

private ArrayList list;
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
try{
SimpleAsyncTask mTask = new SimpleAsyncTask();
mTask.execute(resource);
}catch (Exception e){}


...
}

private String[] mStrings={
"http://resourse1.com",
"http://resourseN.com",
};

@Override
private void onTaskComplete(ArrayList taskList){
list = taskList;
//String[] strArray = new String[ list.size() ];
//int length = strArray.length; // lenght = 0
//mStrings = new String[ list.size() ];
//int length = strArray.length;
//for( int j = 0; j < length; j++ ) {
// mStrings[j] = list.get(j).toString();
//}
//Instead of the above code you can also use this
String[] array = list.toArray(new String[list.size()]);

// Create custom adapter for listview
adapter=new MyImageLoadAdapter(this, array);
}
}

现在按如下方式更改您的 Asynctask:

    public class SimpleAsyncTask extends AsyncTask<String, String, String>{

private OnTaskCompleteListener listener;

public SimpleAsyncTask(OnTaskCompleteListener listener) {
this.listener = listener;
}

protected String doInBackground(String... uri) {
//working code
}


@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
...//Convert the response to list and call your listener
listener.onTaskComplete(list);
// return list //expected value;// no need of it now.
}
}

在包中创建一个接口(interface)。

public interface OnTaskCompleteListener {
void onTaskComplete(ArrayList list);
}

在这里,您在 Activity 中实现一个接口(interface),在创建它时将其传递给异步任务,一旦在异步任务中调用 onpostexecute,您就可以使用传递的接口(interface)对象调用 Activity 中实现的方法。

希望这有帮助。

关于java - Android线程通信AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218815/

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