[英]How can I pass a primitive int to my AsyncTask?
我想要的是將一個int
變量傳遞給我的AsyncTask
。
int position = 5;
我宣布我的AsyncTask是這樣的:
class proveAsync extends AsyncTask<int, Integer, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(int... position) {
}
.
.
.
但我得到一個錯誤,它是以下內容:
類型參數不能是基本類型
我只是傳遞一個int[]
和Integer
變量,但從來沒有一個int
變量,我執行我AsyncTask
是這樣的:
new proveAsync().execute(position);
我能做些什么才能通過這個position
嗎?
提前致謝!
將參數傳遞為Integer
class proveAsync extends AsyncTask<Integer, Integer, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(Integer... position) {
int post = position[0].intValue();
}
.
.
.
執行時執行此操作
new proveAsync().execute(new Integer(position));
您可以使用intValue()
獲取AsyncTask
的int值
像這樣使用它。
class proveAsync extends AsyncTask<Integer, Void, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(Integer... params) {
int position = params[0];
...
傳遞陣列中的位置。 例如:
Integer[] asyncArray = new Integer[1];
asyncArray[0] = position;
new proveAsync().execute(asyncArray);
您還可以使用AsyncTask的構造函數。
class proveAsync extends AsyncTask<Void, Void, Void> {
int position;
public proveAsync(int pos){
position = pos;
}
protected void onPreExecute(){
}
protected Void doInBackground(Void... args) {
}
.
.
然后使用它像:
new proveAsync(position).execute();
並且您可以按照要求傳遞任何內容,而不會以這種方式更改返回類型和參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.