0

I have a question regarding clearing recyclerview after few seconds in android. I tried doing this in a Thread but it didn't work for me. I used this code

public void cleardbView()
{
    final Thread countdown = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(15000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView = (RecyclerView) findViewById(R.id.dbView);
                        arrayList.clear();
                        layoutManager = new LinearLayoutManager(MainActivity.this);
                        return;
                        }
                    });

            } catch (InterruptedException e) {
            }
            return;
        }
    };
    countdown.start();
}

I am using this code in the section where I am adding data. The method is too big with all the conditions so I will put here only pseudo code(I tried to simplify it as much as I could :D )

if(conditions)
{
    insertData();
}
if(inserted)
{
    cleardbView(); 
}
Rahul Chokshi
  • 670
  • 4
  • 18
Apuna12
  • 375
  • 2
  • 6
  • 23
  • no need to alter adapter again and again .. just add and clear data from your arraylist and then notify the adapter – Devil10 Aug 21 '18 at 11:41

2 Answers2

1

You need to use yourAdapter.notifyDataSetChanged(); after Clearing your list arrayList.clear();

Adapter.notifyDataSetChanged();

  • Notify any registered observers that the data set has changed.

Try this

public void cleardbView()
{
    final Thread countdown = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(15000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView = (RecyclerView) findViewById(R.id.dbView);
                        arrayList.clear();
                        layoutManager = new LinearLayoutManager(MainActivity.this);
                        yourAdapter.notifyDataSetChanged();
                        return;
                        }
                    });

            } catch (InterruptedException e) {
            }
            return;
        }
    };
    countdown.start();
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

This is a better and shorter implementation of your problem, and handlers are background threads that provide you ability to communicate with the UI // check here for more explanation about threads and handlers Handler vs AsyncTask vs Thread

Handler handler = new Handler();
int delay = 2000; //milliseconds

handler.postDelayed(new Runnable(){
    public void run(){
       RecyclerViewAdapterDataList.clear();
       RecyclerViewAdapter.notifyDataSetChanged();
    }
}, delay);
Badran
  • 515
  • 1
  • 7
  • 21
  • Isn't that a some kind of delegate? – Apuna12 Aug 22 '18 at 06:51
  • I am sorry, but what do you mean? I didn't understand – Badran Aug 22 '18 at 07:07
  • 1
    https://en.wikipedia.org/wiki/Delegation_(object-oriented_programming) try to check this... very nice type of "not thread" programming :) – Apuna12 Aug 22 '18 at 07:54
  • oh no handlers are background threads , and they are better than using the basic thread (Thread) which is given in first solution – Badran Aug 22 '18 at 11:06
  • Oh ok... I'll try that next time.. for my solution is better to use thread instead for now :)... thx I am upvoting your suggestion :) – Apuna12 Aug 23 '18 at 07:33