开发者

Android: Unable to specify List<String> return type for AsyncTask:doInBackground

开发者 https://www.devze.com 2023-02-13 10:33 出处:网络
I have the following block of code based on an asynctask. I\'m trying to return a List variable via return LoadFeed()

I have the following block of code based on an asynctask. I'm trying to return a List variable via return LoadFeed() and the return type of doInBackground is String. If I change the return type of doInBackground from String to List then I get an error

"The return type is incompatible with AsyncTask<String,Void,String>.doInBackground(String[])"

How do I fix this error? Please help

Thanks,

  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
   prot开发者_JS百科ected 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);

   }
}


Change your class definition to:

class DispData extends AsyncTask<String, Object, List<String>>

This will force the doInBackground declaration to become:

protected List<String> doInBackground(String... arg);


First, you really should add @Override annotations to your onPreExecute(), doInBackground(), and onPostExecute() methods. On an AsyncTask, this is critical to help you keep track of all the data types.

Then, if you want doInBackground() to return List<String>, you need to change it both in doInBackground() and in your AsyncTask declaration (the third data type will need to change from String to List<String>), to go along with your change that you already made to onPostExecute().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号