Android线程通信AsyncTask

Android thread communication AsyncTask

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

任务:而不是在 MainActivity 的私有方法中使用硬编码的 URLs 从放置在 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.
    }
}

在你的包中创建一个接口。

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

此处您在 activity 中实现了一个接口,在创建它时将其传递给异步任务,在异步任务中调用 onpostexecute 后,您将调用 [=23= 中实现的方法] 使用传递的接口对象。

希望对您有所帮助。