I have to update my RecyclerView adapter for every x seconds

1.3k Views Asked by At

I am fetching data from database and showing it into RecyclerView.But I have to update the RecyclerView every x milliseconds/seconds.

Here is my code. Please help.

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    View view = inflater.inflate(R.layout.fragment_download, container, false);

    rvLatestTrack = (RecyclerView) view.findViewById(R.id.recyclerview);
    linearLayoutEmpty = (LinearLayout) view.findViewById(R.id.linearLayoutEmpty);

    arrayList = new ArrayList<>();
    rvLatestTrack.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
    getData();
    return view;
}
public void getData() {
    Database database = new Database(getContext());
    SQLiteDatabase sqLiteDatabase = database.getWritableDatabase();
    String SELECT_DATA_QUERY = "SELECT * FROM " + DB_Const.TABLE_NAME_SONGS;
    Cursor cursor = sqLiteDatabase.rawQuery(SELECT_DATA_QUERY, null);
    if (cursor.getCount() != 0) {
        if (cursor.moveToFirst()) {
            DownloadsModel downloadsModel;
            do {
                String fileName = cursor.getString(cursor.getColumnIndex(DB_Const.SONG_TITLE));
                String Download_percentage = cursor.getString(cursor.getColumnIndex(DB_Const.Completed_percentage));
                String SongURL = cursor.getString(cursor.getColumnIndex(DB_Const.URL));
                downloadsModel = new DownloadsModel(fileName, Download_percentage, SongURL);
                arrayList.add(downloadsModel);
            } while (cursor.moveToNext());
            rvLatestTrack.setAdapter(new DownloadsAdaptor(getContext(), arrayList));
        }
        cursor.close();
    } else {
        linearLayoutEmpty.setVisibility(View.VISIBLE);
    }
}
3

There are 3 best solutions below

11
On

You need to declare your DownloadsAdapter global:

DownloadsAdapter adapter = new DownloadsAdaptor(getContext(), arrayList)

and then

private void update() {
  Handler handler = new Handler();
  handler.postDelayed(new Runnable() {
     @Override
     public void run() {
        arrayList = ...
        adapter.notifyDataSetChanged(); //or notifyItemInserted or notifyItemRemoved as per your need.
        update(); // recursive call
     }
  }, 1000);
}

This will update your list every 1000 microseconds (x time) and notify the RecyclerView adapter of a change in the data.

1
On

In adapter constructor, add a timer to schedule task

TimerTask task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getData();
                    }
                });
            }
        };
new Timer().schedule(task, 0, 3000);
0
On

I recommend you use the in-built AsyncTask

for this:

  • lets you do expensive work on a background thread without causing UI stutter
  • has a onProgressUpdatecallback precisely for updating the UI