在android AsyncTask类中返回一个ArrayList

时间:2013-10-18 09:30:35

标签: android arraylist android-asynctask

我有一个android片段,其中包含一个如下实现的android AsyncTask类

    protected class getGraph extends
        AsyncTask<GraphFragment, Void, GraphFragment> {
    @Override
    protected GraphFragment doInBackground(GraphFragment... params) {
        performance = getPerformance(sharename);
        return null;
    }

    protected void onPostExecute(GraphFragment Param) {

    }

}

现在我想将一个ArrayList的值性能返回给调用方法。我该如何实现呢?

4 个答案:

答案 0 :(得分:0)

试试这个:

(将“String”替换为您的数据的任何本机变量类型)

protected class getGraph extends
    AsyncTask<GraphFragment, Void, List<String>> {

List<String> performance = new ArrayList<String>();

@Override
protected GraphFragment doInBackground(GraphFragment... params) {
    performance = getPerformance(sharename);
    return null;
}

protected void onPostExecute(List<String> Param) {

}

}

答案 1 :(得分:-1)

采取这种方法

protected class getGraph extends
    AsyncTask<GraphFragment, Void, ArrayList<YOUROBJECT> {

    @Override
    protected ArrayList<YOUROBJECT> doInBackground(GraphFragment... params) {
       performance = getPerformance(sharename);
       return performance;
    }

    public void onPostExecute( ArrayList<YOUROBJECT> Param) {

    }
}

在你的其他Class / MainActivity中这样称呼它:

private void getSomething(){
    new getGraph({
        @Override
        void onPostExecute(ArrayList<YOUROBJECT> Param){
            //DO SOMETHING IN MAINACTIVITY
        }
    }).execute(); 
}

答案 2 :(得分:-1)

这样做..

protected class getGraph extends
    AsyncTask<GraphFragment, Void, ArrayList<String> {


    @Override
    protected GraphFragment doInBackground(GraphFragment... params) {
       performance = getPerformance(sharename);
       ArrayList<String> DATA=new ArrayList<String>();
       /*
       do the process and assign result in DATA
       */
       return DATA;
    }

    protected void onPostExecute( ArrayList<String> Param) {
     // Get your arraylist here.
    }
}

答案 3 :(得分:-1)

使用以下代码。

public class calc_stanica extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(baraj_mapa.this);
        dialog.setTitle("Calculating...");
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
    }

    protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
        ArrayList<String> result = new ArrayList<String>();
        ArrayList<String> passed = passing[0]; //get passed arraylist

        // Assign data to your Arralist from your method
         result = getData();
        //Some calculations...do your stuff here.

        return result; //return result arraylist
    }

    protected void onPostExecute(ArrayList<String> result) {
        dialog.dismiss();
       // Get your arraylist here.
    }