2013-01-19 32 views
2

到的AsyncTask数组列表值I具有的AsyncTask设置我传递一个Arraystring这样获得通过在doinbackground

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

的AsyncTask方法 类myasynctask延伸的AsyncTask,整数,字符串>

DOinbackground

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

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

} 

我得到一个数组i ndex债券异常 如何将两个值传入doinbackground方法并同时获得两者。

+0

您能区分“creds”和“credentials”吗? –

回答

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

} 
+0

为什么downvote任何理由? –

0

要获得ArrayList的价值,

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

获取ArrayList中得到的值作为doInBackground方法的AsyncTask的:

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 
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