问题
I am using RecyclerView to display the list of rows, each row has a buy button. when the user buys an element, the method notifydatasetchanged() is called and the buy button has to change the background color for example to red.
Important, The user have to buys only one element, and I have a thread that runs every 5 seconds looking for new answers, if there is a new answer, it is added to the list and scroll to last position.
My problem is when I have the list of buttons and only one is red and I have a new answer, I add it to the list but the button of some rows change to red.
this is my code:
private void responses() {
fin_thread = true;
thread = new Thread() {
@Override
public void run() {
while (fin_thread) {
try {
Thread.sleep(5000);
runOnUiThread(new Runnable() {
@Override
public void run() {
JsonObjectRequest service_call = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
try {
boolean status = response.getBoolean("value_status_response");
if (status) {
if (response.getInt("counter") > 0 && obj_response.size() > 0) {
JSONArray answer = response.getJSONArray("answer");
if (answer.length() != obj_response.size()) {
for (int i = 0; i < answer.length(); i++) {
boolean coincidence = false;;
JSONObject item = answer.getJSONObject(i);
for (int j = 0; j < obj_response.size(); j++) {
if (item.getId("id") == obj_response.get(j).getId()) {
coincidence = true;
break;
}
}
if (!coincidence) {
CResponse cr = new CResponse();
cr.setName(item.getString("name"));
cr.setPrice(item.getString("price"));
cr.setId(item.getInt("id"));
cr.setState(item.getInt("state"));
obj_response.add(cr);
sectionAdapter.notifyDataSetChanged();
scroll_to_last();
}
}
}
} else {
if (response.getInt("counter") > 0 && obj_response.size() == 0) {
JSONArray answer = response.getJSONArray("answer");
for (int i = 0; i < answer.length(); i++) {
JSONObject item = answer.getJSONObject(i);
CResponse cr = new CResponse();
cr.setName(item.getString("name"));
cr.setPrice(item.getString("price"));
cr.setId(item.getInt("id"));
cr.setState(item.getInt("state"));
obj_response.add(cr);
sectionAdapter.notifyDataSetChanged();
rv.setVisibility(View.VISIBLE);
progress_bar.setVisibility(View.GONE);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
//ERROR
}
}
}
) {
@Override
public Map < String, String > getHeaders() throws AuthFailureError {
Map < String, String > params = new HashMap < String, String > ();
params.put("Authorization", "Basic " + auth);
return params;
}
};
Helpers.queue(Class.this).add(service_call);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread.start();
}
private void scroll_to_last() {
lm.scrollToPosition(sectionAdapter.getItemCount() - 1);
}
My Adapter
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
final MyViewPedidoDesc itemHolder = (MyViewPedidoDesc) holder;
control_btn_background(itemHolder, position);
}
private void control_btn_background(MyViewPedidoDesc itemHolder, int position) {
if (mData.get(position).getState() == 1) {
itemHolder.buy_btn.setBackgroundResource(R.drawable.red);
} else {
itemHolder.buy_btn.setBackgroundResource(R.drawable.blue);
}
}
I think the error appears is when I call scroll_to_last()
method because if I don't call that the background color is only in one button.
Thanks in advace
来源:https://stackoverflow.com/questions/56692194/error-notifydatasetchanged-in-recyclerview-when-its-scroll-to-last-position