2013-01-19 82 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