Android: Cannot specify return type <String> for AsyncTask: doInBackground

I have the following code based on asynctask code. I am trying to return a List variable using return LoadFeed () and the return type doInBackground is a string. If I change the return type doInBackground from String to List, then I get an error

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

How to fix this error? Please, help

Thank,

  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);

   }
}
+3
source share
2 answers

Change the class definition to:

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

This will make the doInBackground declaration become:

protected List<String> doInBackground(String... arg);
+16
source

-, @Override onPreExecute(), doInBackground() onPostExecute(). AsyncTask , .

, doInBackground() List<String>, doInBackground(), AsyncTask ( String List<String>), , onPostExecute().

+1

Source: https://habr.com/ru/post/1794963/


All Articles