I'm using Alarm Manager to update recycler view on specific interval (say in 10 seconds). I'm able to update data in recycler data adapter but recycler view not getting updated.
public class UpdateService extends IntentService {
public UpdateService() {
super("UpdateService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Do the task here
Call<List<DataMessage>> userCall = MainActivity.serviceApi.getDataList();
userCall.enqueue(new Callback<List<DataMessage>>() {
@Override
public void onResponse(Call<List<DataMessage>> call, Response<List<DataMessage>> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
for (DataMessage DataMessage : response.body()) {
if (DataMessage.getMessageId() > 0)
DummyContent.DataMessages.add(DataMessage);
}
MyDataRecyclerViewAdapter myDataRecyclerViewAdapter = new MyDataRecyclerViewAdapter(DummyContent.DataMessages);
myDataRecyclerViewAdapter.notifyDataSetChanged();
// Here I want something like:
if (myDataRecyclerViewAdapter.getItemCount() > 2)
recyclerView.smoothScrollToPosition(myDataRecyclerViewAdapter.getItemCount() - 1);
MainActivity.appPreference.showToast("message fetched");
} else {
MainActivity.appPreference.showToast("Error. message fetch Failed");
}
} else {
MainActivity.appPreference.showToast("Error. message Failed");
}
}
@Override
public void onFailure(Call<List<DataMessage>> call, Throwable t) {
MainActivity.appPreference.showToast("Error. message Failed");
}
});
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class DataAlarmReceiver extends BroadcastReceiver {
// Triggered by the Alarm periodically (starts the service to run task)
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, UpdateService.class);
//i.putExtra("foo", "bar");
context.startService(i);
}
}
When updating data adapter in recycler fragment activity then working fine with following:
myDataRecyclerViewAdapter.notifyDataSetChanged();
// Here I want something like:
if (myDataRecyclerViewAdapter.getItemCount() > 2)
recyclerView.smoothScrollToPosition(myDataRecyclerViewAdapter.getItemCount() - 1);
I want to know if we can get recyclerview object in alarm service class?
Views:
3641
Total Answered:
1
Total Marked As Answer:
1
Posted On:
27-Dec-2019 21:56