【问题标题】:how to get the result in onPostExecute method如何在 onPostExecute 方法中获取结果
【发布时间】:2017-06-05 09:50:23
【问题描述】:

我正在尝试从模型中获取 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();


    }
}

【问题讨论】:

  • 这是个糟糕的问题吗??有人投了反对票。

标签: android json model-view-controller android-sdk-manager


【解决方案1】:

检查一下

 @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();
      }
    }
}

【讨论】:

    【解决方案2】:

    您正在尝试从新创建的没有任何内容的 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();
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2020-01-26
      • 2017-01-12
      • 2015-08-19
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多