获取在doinbackground传递给asynctask的arraylist值

时间:2013-01-19 11:07:02

标签: java android arrays android-asynctask

我有一个Asynctask设置我传递了像这样的Arraystring

ArrayList<String> creds = new ArrayList<String>();
creds.add(string1);
creds.add(string2);
new myasynctask().execute(credentials);

asynctask方法 class myasynctask扩展了AsyncTask,Integer,String&gt;

DOinbackground

protected String doInBackground(ArrayList<String>... creds) 

            {       String lol1= creds[0].toString();
                    String lol2= creds[1].toString();
........rest of the code

}

我从债券异常中得到一个数组索引 如何将两个值传递给doinbackground方法并立即获取它们。

4 个答案:

答案 0 :(得分:5)

您可以通过此代码获得价值

class test extends AsyncTask<ArrayList<String>, Void, Void>
{

@Override
protected Void doInBackground(ArrayList<String>... creds) {
    // TODO Auto-generated method stub
    String str1 = creds[0].get(0);
    String str2 = creds[0].get(1);

    return null;
}

}

答案 1 :(得分:0)

获取arraylist值,

 ArrayList<String> recList= new ArrayList<String>();
        ArrayList<String> arrayListVal= creds[0]; 

答案 2 :(得分:0)

在AsyncTask的doInBackground方法中获取arraylist:

private class myAsyncTask extends AsyncTask<ArrayList<String>, Void, String> {

      @Override
      protected String doInBackground(ArrayList<String>... params) {

       ArrayList<String>  lol1= params[0]; //<< here retrieve Array from  varargs
      return null;
      }      
         // your code here

答案 3 :(得分:0)

请尝试以下链接

Passing arguments to AsyncTask, and returning results

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

        //Some calculations...

        return result; //return result