无法调用runOnUiThread()方法

5
我有一个名为MyRequestFragment的片段,其中包含一个RecyclerView,并使用适配器类将数据绑定到RecyclerView上的onBindViewHolder()。现在,每个ViewHolder都有一个按钮。每次点击按钮时,向服务器发送POST请求并更新值。我的问题是,如果成功响应,则需要隐藏特定ViewHolder上的按钮。实际问题是runOnUiThread()不接受onBindViewHolder()
runOnUiThread(new Runnable() {
    public void run() {

    }
});

如何在ViewHolder的创建/绑定方法中调用这个东西。

下面是我完整的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();
                }
            }
        });
    }

可能是重复问题:很遗憾,MyApp已停止运行。我该如何解决? - Vladyslav Matviienko
please include the error log - lal
@lal 实际上,这段代码无法编译。因为 runOnUiThread 是语法错误。它会显示为红色。 - nifCody
2个回答

7

runOnUiThread()Activity的一个方法。

如果您可以访问到Activity实例,则直接在其上调用该方法;否则,使用Handler将您的Runnable提交到主线程的消息队列中:

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

0

在调用runOnUiThread之前,您应该获取您的活动:

getActivity.runOnUiThread(new Runnable(){
            @Override
            public void run(){
                 /*your code*/
            }
});

请使用 ``` 来输入代码块,这将使你的回答更易读。 - Ankit Kante

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接