ArrayList<Integer>
我希望将其传递给
AsyncTask<ArrayList<Integer>, void, void>
。
但在
doInBackground(ArrayList<Integer>...params)
函数,
我无法接收我传递的arrayList。
在doInBackground
内,我使用ArrayList<Integer> arr = params[0]
,然后log(arr.size()) is 0
我的代码:
class count extends AsyncTask<Void, Integer, ArrayList<Integer>>{
ArrayList<Integer> arr = new ArrayList<Integer>();
ArrayList<Integer> temp = new ArrayList<Integer>();
@SuppressWarnings("unchecked")
@Override
protected ArrayList<Integer> doInBackground(Void... params) {
// TODO Auto-generated method stub
for(int i = 1; i <= 100; i++){
SystemClock.sleep(200);
arr.add(i);
if(i % 10 == 0){
temp = arr;
//Log.d("DEBUG", "Length of temp = "+ temp.size());
arr.clear();
mean task1 = new mean();
task1.execute(temp);
}
publishProgress(i);
}
return arr;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
tvNum.setText(values[0]+"");
}
}
class mean extends AsyncTask<ArrayList<Integer>, Integer, ArrayList<Integer>>{
@Override
protected ArrayList<Integer> doInBackground(
ArrayList<Integer>... params) {
// TODO Auto-generated method stub
ArrayList<Integer> arrL =new ArrayList<Integer>();
arrL= params[0];
Log.d("DEBUG","iNPUT Size = " + arrL.size());
return null;
}
}
请帮帮我, 感谢。
答案 0 :(得分:1)
简单的例子供您理解。比如
public class MainActivity extends Activity {
List<CalcPrimesTask> taskList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
taskList = new ArrayList<CalcPrimesTask>();
}
public void onClickStart(View view) {
EditText maximumEditText = (EditText) findViewById(R.id.maximumEditText);
int maxNum = Integer.parseInt(maximumEditText.getText().toString());
CalcPrimesTask task = new CalcPrimesTask(this);
taskList.add(task);
task.execute(maxNum);
Toast.makeText(getApplicationContext(), "New run queued.", Toast.LENGTH_SHORT).show();
}
public void onStopClick(View view) {
for (CalcPrimesTask task : taskList) {
task.cancel(true);
}
Toast.makeText(getApplicationContext(), "All runs cancelled.", Toast.LENGTH_SHORT).show();
}
}
和
public class CalcPrimesTask extends AsyncTask<Integer, String, List<Integer>> {
Activity activity;
public CalcPrimesTask(Activity mainActivity) {
activity = mainActivity;
}
@Override
protected List<Integer> doInBackground(Integer... params) {
int maxNum = params[0];
List<Integer> primeList = new ArrayList<Integer>();
for (int i = 2; i <= maxNum ; i++) {
int maxCalc = (int)Math.sqrt(i);
boolean isPrime = true;
for (int j = 2; j <= maxCalc ; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeList.add(i);
publishProgress("Prime " + i + " found.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
return primeList;
}
@Override
protected void onProgressUpdate(String... values) {
TextView messageView = (TextView) activity.findViewById(R.id.messageText);
messageView.setText(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(List<Integer> result) {
TextView messageView = (TextView) activity.findViewById(R.id.messageText);
messageView.setText("Total of " + result.size() + " primes found.");
super.onPostExecute(result);
}
}
如果您的手可用时间,请阅读Android AsyncTask。运气最好!
答案 1 :(得分:0)
如果在调用execute()时将Arraylist作为唯一参数传递,则它应该在params [0]中。例如,
YourTask.execute(YourList);
你可以在ASyncTask里面访问它:
Arraylist<Integer> myList = params[0];