【问题标题】:how to wait for the complete execution of asynctask in using onbindviewholder?如何在使用 onbindviewholder 时等待 asynctask 的完整执行?
【发布时间】:2017-06-16 07:54:49
【问题描述】:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    User user = mUsers.get(position);
    String currentuser = FirebaseAuth.getInstance().getCurrentUser().getEmail();
    new AddTodatabase4(user.email,currentuser).execute();
    System.out.println("checking check"+check);

    if(check==1){
        holder.status.setText("message");}

    String alphabet = user.email.substring(0, 1);

    holder.txtUsername.setText(user.email);
    holder.txtUserAlphabet.setText(alphabet);

  //  System.out.println(currentuser+" "+user.email);


}

这里,addtodatabase4 扩展了 asynctask 并且 check 是一个全局变量。我希望保留 onbindviewholder 的其余执行,直到 addtodatabase4 完成其执行,因为在使用 settext 设置我的状态文本视图时我需要检查的修改值。如何实现这一点。使用此代码 addtodatabse4 在最后执行。

【问题讨论】:

  • 把任务完成后想做的事情放到任务的onPostExecute中。

标签: android android-asynctask android-recyclerview


【解决方案1】:

我将整个视图传递给异步任务,然后在检查条件后将我的状态文本视图设置在那里。

【讨论】:

    【解决方案2】:

    创建一个标志:

    public static boolean isDownloadFinished = false;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            isDownloadFinished = false;
        }
    

    onPostExecute 内设置isDownloadFinished 为true

     @Override
     protected void onPostExecute(Boolean success) {
      super.onPreExecute();
       if (success) {
       isDownloadFinished = true;
      }else{
          isDownloadFinished = false;
        }
    }
    

    然后:

    if(YourClass.isDownloadFinished){
        // do your task here
    }
    

    【讨论】:

    • 甚至 asynctask.get 不工作...onbindview 函数首先设置不同的视图..
    • 你在哪里打电话recyclerView.setAdapter(adapter);
    • onPostExecute中设置你的适配器。
    • 我每次都需要将 user.email 传递给 addtodatabse4,所以无法在 postexecute 中设置我的适配器。
    【解决方案3】:

    尝试将 holder 传递给异步任务,例如

    new AddTodatabase4(user.email,currentuser, holder).execute();
    

    并在 AsyncTask 的 onPostCreate 的 holder 中设置 textViews。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-17
      • 2020-09-18
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      相关资源
      最近更新 更多