Android线程通信AsyncTask

时间:2016-03-25 11:11:23

标签: java android json android-asynctask runnable

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

任务:而不是在MainActivity中的私有方法中获取硬编码的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;
    }
}

1 个答案:

答案 0 :(得分:1)

你可以这样做:

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);
}

在这里,您要在活动中实现一个接口,在创建它时将其传递给异步任务,一旦在异步任务中调用onpostexecute,您将使用传递的接口对象调用活动中实现的方法。

希望这有帮助。