gpt4 book ai didi

android - 无法调用 runOnUiThread()

转载 作者:搜寻专家 更新时间:2023-11-01 08:27:45 25 4
gpt4 key购买 nike

我有一个 fragment 叫做 MyRequestFragment ,其中包含一个 RecyclerView并将数据绑定(bind)到 RecyclerView使用 onBindViewHolder()使用我的适配器类。

现在每个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();
}
}
});
}

最佳答案

runOnUiThread()Activity的一个方法。

要么在 Activity 实例上调用它(如果您有权访问一个实例),要么使用 Handler 将您的 Runnable 发布到主线程的消息队列:

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

关于android - 无法调用 runOnUiThread(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43183027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com