我有这些字符串latRepr和LngRepr,我想将它们发送到getDirecoes方法。 GetDirecoes类是AsyncTask。
这就是我启动方法的方法:
new GetDirecoes()。execute();
那么,我该如何将这些字符串发送到这里:
private class GetDirecoes extends AsyncTask<Void, Void, Void> implements Serializable {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {}
答案 0 :(得分:3)
new GetDirecoes().execute(latRepr,LngRepr);
这是你可以通过的方式。
private class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
String latRepr=params[0];
String LngRepr=params[1];
}
}
答案 1 :(得分:1)
试试这个
new GetDirecoes().execute(latRepr,LngRepr);
之后
private class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
String latRepr = params[0];
String LngRepr= params[1];
}
答案 2 :(得分:0)
在AsyncTask中使用构造函数
private class GetDirecoes extends AsyncTask<Void, Void, Void> implements Serializable {
String mLat;
String mLng;
public GetDirecoes(String lat, String lng){
mLat = lat;
mLng = lng;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
}
并传递值
new GetDirecoes(latRepr,LngRepr).execute();
答案 3 :(得分:0)
您的第一个Void
代表doInBackground
方法中的参数类型。
所以你可以构建你的AsyncTask
:
private class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
String lat = params[0];
String lng = params[1];
...
}
然后像这样调用你的AsyncTask:
new GetDirecoes().execute(latRepr, lngRepr);
您可以在那里找到更多信息:https://developer.android.com/reference/android/os/AsyncTask.html
答案 4 :(得分:0)
你的GetDirecoes是一个课程,而不是一种方法。
你可以做的是创建一个构造函数,它接受两个String参数,并将变量存储为GetDircoes的字段。
private class GetDirecoes {
private String latRepr;
private String LngRepr;
public GetDirecoes(String latRepr, String LngRepr) {
this.latRepr = latRepr;
this.LngRepr = LngRepr;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {}
}
然后你应该可以致电new GetDirecoes(latRepr,LngRepr).execute();
答案 5 :(得分:0)
AsyncTask
具有通用类型,在您的情况下为Void, Void, Void
。这3种类型是方法的参数
doInBackground(params...)
onProgressUpdate(params...)
和
onPostExecute(Result)
因此,如果您的第一个类型是String(AsyncTask<String, Void, Void>
),那么您的doInBackground
方法看起来就像
doInBackground(String... params) {
String firstparam = params[0];
}
请考虑检查params是否为空并且大小是否大于0
所有记录均为here