[英]How can I pass a primitive int to my AsyncTask?
What I want it's to pass one int
variable to my AsyncTask
. 我想要的是将一个
int
变量传递给我的AsyncTask
。
int position = 5;
And I declared my AsyncTask like this: 我宣布我的AsyncTask是这样的:
class proveAsync extends AsyncTask<int, Integer, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(int... position) {
}
.
.
.
But I got an error that it's the following: 但我得到一个错误,它是以下内容:
Type argument cannot be of primitive type
类型参数不能是基本类型
I just could pass an int[]
and Integer
variables but never an int
variable and I execute my AsyncTask
like this: 我只是传递一个
int[]
和Integer
变量,但从来没有一个int
变量,我执行我AsyncTask
是这样的:
new proveAsync().execute(position);
Is there something that I could do to pass only this position
? 我能做些什么才能通过这个
position
吗?
Thanks in advance! 提前致谢!
Pass your parameter as Integer
将参数传递为
Integer
class proveAsync extends AsyncTask<Integer, Integer, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(Integer... position) {
int post = position[0].intValue();
}
.
.
.
while executing do this 执行时执行此操作
new proveAsync().execute(new Integer(position));
You can get the int value in AsyncTask
as using intValue()
您可以使用
intValue()
获取AsyncTask
的int值
Use it like this. 像这样使用它。
class proveAsync extends AsyncTask<Integer, Void, Void> {
protected void onPreExecute(){
}
protected Void doInBackground(Integer... params) {
int position = params[0];
...
Pass position in an array. 传递阵列中的位置。 eg:
例如:
Integer[] asyncArray = new Integer[1];
asyncArray[0] = position;
new proveAsync().execute(asyncArray);
You can also use constructor of AsyncTask . 您还可以使用AsyncTask的构造函数。
class proveAsync extends AsyncTask<Void, Void, Void> {
int position;
public proveAsync(int pos){
position = pos;
}
protected void onPreExecute(){
}
protected Void doInBackground(Void... args) {
}
.
.
then use it like: 然后使用它像:
new proveAsync(position).execute();
and you can pass anything as of requirement without changing return type and arguments in this way.. 并且您可以按照要求传递任何内容,而不会以这种方式更改返回类型和参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.