0

I have card views in a gridlayoutmanager.I have implemented on swipe ie when I swipe up the card gets dismissed.My app was working well until I put that code in a new thread to increase performance. When I did it the card is getting removed but it is again showing up after 2-3 seconds. initially when I swiped up the card would get dismissed and next card would show up.

My code:

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.UP )
        {

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
            {
                Toast.makeText(getApplicationContext(), "on Move", Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public void onSwiped(final RecyclerView.ViewHolder viewHolder, int swipeDir)
            {

                Toast.makeText(getApplicationContext(), "Task Status changed to COMPLETE", Toast.LENGTH_LONG).show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String username = getIntent().getStringExtra("username");
                        String password = getIntent().getStringExtra("password");
                        try {
                            restApi=new RallyRestApi(new URI("https://rally1.rallydev.com"),username,password);
                            JsonObject updatedValues = new JsonObject();
                            updatedValues.addProperty("State", "Completed");
                            UpdateRequest taskUpdate = new UpdateRequest(data_list.get(viewHolder.getAdapterPosition()).getRef(), updatedValues);
                            restApi.update(taskUpdate);
                            data_list.remove(viewHolder.getAdapterPosition());
                            adapter.notifyDataSetChanged();
                        } catch (URISyntaxException | IOException e) {
                            e.printStackTrace();
                        }


                    }
                });
            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
Akshay
  • 1,161
  • 1
  • 12
  • 33

2 Answers2

0

You should call adapter.notifyDataSetChanged() only on the main thread(UI thread).

Refer this SO thread

Community
  • 1
  • 1
arjun
  • 3,514
  • 4
  • 27
  • 48
0

Use runOnUiThread() method to execute the UI action from a Non-UI thread.

private class ReceiverThread extends Thread {
@Override
public void run() { 
Activity_name.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
             mAdapter.notifyDataSetChanged();
        }
    });
}

You can not touch the views of the UI from other thread. For your problem you can use either AsyncTask, runOnUiThread or handler.