如何在 onPostExecute 方法中获取结果

how to get the result in onPostExecute method

我正在尝试从模型中获取 getcontent() 方法,但是当我尝试获取该方法时它显示空值。

public class Gettingcomment extends AsyncTask<String,String,List<CommentModel>>{


    @Override
    protected List<CommentModel> doInBackground(String... params) {

        String commenturl = params[0];

        Populatecomments populatecomments =new Populatecomments();
       // populatecomments.getCommentModelList(commenturl+mytrendinid);

        Log.i("mytrendin",commenturl);

        List<CommentModel>  model= populatecomments.getCommentModelList(commenturl);

        return model;
    }

    @Override
    protected void onPostExecute(List<CommentModel> results) {
        super.onPostExecute(results);
        CommentModel commentModel = new CommentModel();
        String content = commentModel.getContent();
        Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG).show();


    }
}

检查这个

 @Override
protected void onPostExecute(List<CommentModel> results) {
    super.onPostExecute(results);
    if(results.size()>0){
    for(int i=0;i<results.size();i++){
        CommentModel commentModel = results.get(i);
        String content = commentModel.getContent();
        Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG).show();
      }
    }
}

您正在尝试从新创建的没有任何内容的 CommentModel 实例中获取内容。所以请从 doinBackground 结果中检查这个。

public class Gettingcomment extends AsyncTask<String,String,List<CommentModel>>{


@Override
protected List<CommentModel> doInBackground(String... params) {

    String commenturl = params[0];

    Populatecomments populatecomments =new Populatecomments();
   // populatecomments.getCommentModelList(commenturl+mytrendinid);

    Log.i("mytrendin",commenturl);

    List<CommentModel>  model= populatecomments.getCommentModelList(commenturl);

    return model;
}

@Override
protected void onPostExecute(List<CommentModel> results) {
    super.onPostExecute(results);
    if(results!=null && results.size()>0){
    //here i am getting getContent from 0 position of CommentModel list you can loop to get all the model's content
    String content = results.get(0).getContent();
    Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG).show();
    }
}

}