我有以下基于asynctask的代码块。 我试图通过返回LoadFeed()返回一个List变量 并且doInBackground的返回类型是String。 如果我将doInBackground的返回类型从String更改为List 然后我收到错误
"The return type is incompatible with AsyncTask<String,Void,String>.doInBackground(String[])"
如何修复此错误? 请帮忙
谢谢,
private class DispData extends AsyncTask<String, Void, String> {
private final ProgressDialog dialog = new ProgressDialog(MessageList.this);
// can use UI thread here
protected void onPreExecute() {
dialog.setMessage("Fetching scores...");
dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected String doInBackground(final String... args) {
return loadFeed();
}
// can use UI thread here
protected void onPostExecute(final List<String> result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
adapter =
new ArrayAdapter<String>(MessageList.this,R.layout.row,result);
MessageList.this.setListAdapter(adapter);
}
}
答案 0 :(得分:16)
将班级定义更改为:
class DispData extends AsyncTask<String, Object, List<String>>
这将强制doInBackground声明变为:
protected List<String> doInBackground(String... arg);
答案 1 :(得分:1)
首先,您确实应该为@Override
,onPreExecute()
和doInBackground()
方法添加onPostExecute()
注释。在AsyncTask
上,这对于帮助您跟踪所有数据类型至关重要。
然后,如果您希望doInBackground()
返回List<String>
,则需要在doInBackground()
和AsyncTask
声明中更改它(第三种数据类型需要从String
更改为List<String>
),以及您对onPostExecute()
所做的更改。