2017-04-03 51 views
0

我有一個叫做MyRequestFragment的片段,它包含RecyclerView,並使用我的適配器類將數據綁定到RecyclerView,使用onBindViewHolder()無法調用runOnUiThread()

現在每ViewHolder我有一個按鈕。並且每按一下按鈕,POST請求就會觸發到服務器並更新一個值。我的問題是,如果成功的迴應來了,我需要隱藏特定的ViewHolder上的按鈕。

的實際問題是runOnUiThread()未在onBindViewHolder()

runOnUiThread(new Runnable() { 
    public void run() { 

    } 
}); 

接受我怎麼把這稱爲ViewHolder中創建/ bind方法。

我的onBindViewHolder()完整代碼作爲參考。

@Override 
    public void onBindViewHolder(MyServiceViewHolder holder, int position) { 
     final MyServiceBean myServiceBean = serviceList.get(position); 
     holder.service_name.setText(myServiceBean.getService_name()); 
     holder.last_updated.setText("Job Updated Date : " +myServiceBean.getDate(myServiceBean.getUpdated_at())); 
     holder.created_date.setText("Job Created Date : " + myServiceBean.getDate(myServiceBean.getCreated_at())); 
     holder.status.setText(myServiceBean.getStatus()); 
     holder.service_note.setText("Service Note : " + myServiceBean.getService_note()); 
     if (myServiceBean.getService_note().toString().isEmpty()) 
      holder.service_note.setVisibility(View.GONE); 

     switch (myServiceBean.getStatus().toString().toLowerCase()) { 
      case "completed": 
       holder.status.setBackgroundColor(Color.GREEN); 
       break; 
      case "on progress": 
       holder.status.setBackgroundColor(Color.YELLOW); 
       break; 
      case "canceled": 
       holder.status.setBackgroundColor(Color.RED); 
       break; 
      case "rejected": 
       holder.status.setBackgroundColor(Color.RED); 
       break; 
      case "accept": 
       holder.status.setBackgroundColor(Color.BLUE); 
       break; 
      default: 
       holder.status.setBackgroundColor(Color.GRAY); 
       break; 
     } 

     if(myServiceBean.getEst_amount() != null) { 
      holder.estimation_region.setVisibility(View.VISIBLE); 
      holder.est_status.setText("Estimation is Available"); 
      holder.btn_approve.setVisibility(View.VISIBLE); 

      holder.est_amount.setText("Estimation Amount: " + myServiceBean.getEst_amount()); 
      if(myServiceBean.getEst_note() != null) 
       holder.est_note.setText("Estimation Note: " + myServiceBean.getEst_note()); 
      if(myServiceBean.getEst_presented_by() != null) 
       holder.est_presented_by.setText("Estimation Prepared By: " + myServiceBean.getEst_presented_by()); 

      if(myServiceBean.getEst_approved_by_customer() == "true"){ 
       holder.btn_approve.setVisibility(View.GONE); 
       holder.est_status.setText("Estimation Approved on : " + myServiceBean.getEst_approved_date()); 
      }else{ 
       holder.btn_approve.setVisibility(View.VISIBLE); 
      } 

     }else{ 
      holder.estimation_region.setVisibility(View.GONE); 
      holder.est_status.setText("Estimation on Process"); 
      holder.btn_approve.setVisibility(View.GONE); 
     } 

     holder.btn_approve.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       updateRequest(); 
      } 
     }); 




    } 

    private void updateRequest() { 
     JSONObject jsonObject = new JSONObject(); 
     try { 
      jsonObject.put("est_approved_by_customer", true); 
      jsonObject.put("est_approved_date", "2017-10-30"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     //progress_dialog.show(); 
     OkHttpClient client = new OkHttpClient(); 

     MediaType JSON = MediaType.parse("application/json; charset=utf-8"); 
     okhttp3.RequestBody body = RequestBody.create(JSON, jsonObject.toString()); 


     okhttp3.Request request = new Request.Builder() 
       .url(ApplicationContants.BASE_URL + ApplicationContants.MY_SERVICE_APPROVE_URL) 
       .post(body) 
       .build(); 

     client.newCall(request).enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, final IOException e) { 

      } 

      @Override 
      public void onResponse(Call call, Response response) throws IOException { 
       try { 
        String responseString = response.body().string(); 

        JSONObject jsonObject = new JSONObject(responseString); 
        Gson gson = new Gson(); 
        final MyServiceBean myServiceBean = gson.fromJson(jsonObject.toString(), MyServiceBean.class); 

        runOnUiThread(new Runnable() { 
         public void run() { 

         } 
        }); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
+0

的[可能的複製不幸的是MyApp的有停止。如何解決這個問題?](http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this) –

+0

請包括錯誤日誌 – lal

+0

@lal其實代碼沒有編譯。因爲runOnUiThread是一個語法錯誤。它顯示爲紅色 – nifCody

回答

2

runOnUiThread()Activity的方法。

或者把它上一個Activity實例(如果你有機會到一個),或使用HandlerRunnable張貼到主線程的消息隊列:

new Handler(Looper.getMainLooper()).post(new Runnable() { 
    public void run() { 
     // do something 
    } 
});