2

I see a similar question here AnimationDrawable in RecyclerView item not animating on notifyDataSetChanged. But couldn't resolve this issue or fix. I think its because notifyDataSetChanged call multiple times to update UI. While I comment or notification change call for the position it works fine. I have three items in the recycled view.

  1. Progress bar
  2. Text view
  3. Button

I need to call an API on button click and increment the textview value. while API calls I need to show the progress bar and hide while API finish

Button click as follow,

holder.image_add_overlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            holder.progressBar2.setVisibility(View.VISIBLE);             
            UpdateProductItem(holder, position);
        }
    });

private void UpdateProductItem(ShoppinglistAdapter.ViewHolder holder, int position) {
    try {
        Boolean value = CartFunctions.UpdateCartPostValue(cartAdd, mAPIService);
        if (value) {
            holder.text_item_cart_count.setText("" + holder.cart_count_value);
                // notifyDataSetChanged();
                holder.progressBar2.setVisibility(View.GONE);
                Task task = new Task();
                task.execute(); 
            Toast.makeText(context, context.getResources().getString(R.string.cart_update_toast_message), Toast.LENGTH_SHORT).show();
        } else {
            holder.cart_count_value = holder.cart_count_value - 1;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

I also try the

@SuppressLint("StaticFieldLeak")
public class Task extends AsyncTask<String, Integer, Integer> {

    @Override
    protected Integer doInBackground(String... strings) {
        Log.i("test", "doInBackground");
        // notifyDataSetChanged();
        return null;
    }

    @Override
    protected void onPostExecute(Integer integer) {
        Log.i("test", "onPostExecute");
        notifyDataSetChanged();
        super.onPostExecute(integer);
    }
}

And call the this using

Task task = new Task();
//task.doInBackground();
task.onPostExecute(0);

May the 2 item in recyclerview are same, so the count is shown to both become same. so i need notifyDataSetChanged() instead of notifyItemChanged(position); Did any guys know the solution?

Subin Babu
  • 1,515
  • 2
  • 24
  • 50

1 Answers1

1

doInBackground() method works in a separate thread, not even on UI thread.

Notify your adapter on UI thread, From AsyncTask : onPreExecute() & onPostExecute() method works on UI thread.

public class Task extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        adapter.notifyDataSetChanged();
    }
}

Call your Task using,

Task task = new Task();
task.execute();

Refer AsynkTask for more details.

buzzingsilently
  • 1,546
  • 3
  • 12
  • 18