我想在用户单击回收器视图项中存在的按钮时发送POST请求。当用户单击按钮时,我正在使用OkHttp3发出网络请求,我正在向节点服务器发送POST请求。 我的问题是在OkHttp3的OnResponse调用中,我无法定义runOnUiThread()方法。
这是我到目前为止所做的:
MyPostedBookAdapter.java
public class MyPostedBookAdapter extends RecyclerView.Adapter<MyPostedBookAdapter.ViewHolder> {
List<PostedModel> listItem;
Context context;
String id;
private static final String URI = "https:bookbudiapp.herokuapp.com/deleteRow";
public MyPostedBookAdapter(List<PostedModel> listItem, Context context){
this.listItem = listItem;
this.context = context;
}
@NonNull
@Override
public MyPostedBookAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.posted_book,viewGroup,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final MyPostedBookAdapter.ViewHolder viewHolder, final int i) {
final PostedModel model = listItem.get(i);
id = model.getPostId();
viewHolder.userBookName.setText(model.getPurchaseBookName());
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.openbook);
Glide.with(context).load(model.getPurchaseImage()).apply(requestOptions).into(viewHolder.userPostBook);
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,id,Toast.LENGTH_SHORT).show();
deleteRow();
}
});
}
@Override
public int getItemCount() {
return listItem.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView userPostBook;
TextView userBookName;
Button delete;
public ViewHolder(@NonNull View itemView) {
super(itemView);
userPostBook = (itemView).findViewById(R.id.userPostBook);
userBookName = (itemView).findViewById(R.id.userBookName);
delete = (itemView).findViewById(R.id.delete);
}
}
private void deleteRow(){
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20,TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.build();
RequestBody formBody = new FormBody.Builder().add("postId",id).build();
Request request = new Request.Builder().url(URI).post(formBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
}
@Override
public void onFailure(Call call, IOException e) {
}
});
}
}
让我知道如何在响应调用中定义runOnUiThread()方法。 任何帮助将不胜感激。
谢谢
答案 0 :(得分:0)
通过您的活动上下文,您可以在适配器类中调用runOnUiThread()。
答案 1 :(得分:0)
通过观察您在此处发布的代码,您要在删除数据后更新UI(RecyclerView上的“ Particuler Row”)。是吗?
您将使用 MyPostedBookAdapter.java 上的Okhttp将日期发送到节点服务器。 现在,更新到UI,您必须在Adaper行onResponse()方法上通知notifydatasetChanged。编写此代码。希望对您有帮助。
private void deleteRow(){
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20,TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.build();
RequestBody formBody = new FormBody.Builder().add("postId",id).build();
Request request = new Request.Builder().url(URI).post(formBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
// To remome row from recyclerView Row(item).
arrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, arrayList.size());
}
@Override
public void onFailure(Call call, IOException e) {
}
});
}
position 是特定行(项目)的位置。要获取Position,可以使用 getAdapterPosition()方法。
position=ViewHolder.getAdapterPosition()
这里有任何查询评论。这对我有帮助。希望对您有帮助。谢谢...