[英]Periodic update items on a RecyclerView
I have an Activity with a Recycler view handling cards. 我有一个带有Recycler视图处理卡的活动。 I have a SwipeRefreshLayout so when I swipe it updates the RecyclerView with new content.
我有一个SwipeRefreshLayout,所以当我滑动它时,会使用新内容更新RecyclerView。 So far, so good.
到现在为止还挺好。
Howerver, I want to update the RecyclerView every X seconds so if, for instance, I leave the activity with the recylcerview opened and I forgot to swipe it, it would as well update by itself. 但是,我想每隔X秒更新RecyclerView,因此,例如,如果我在打开活动recylcerview的情况下忘了滑动它,那么它也会自行更新。 To do that, I thought something like this: ( I ommitted necessary code ).
为此,我想这样的事情:(我省略了必要的代码)。
My main Activity which contains the recyclerview schedules a Job like this: 我的主要活动(包含recyclerview)安排了一个作业,如下所示:
private void scheduleJob() {
ComponentName serviceName = new ComponentName(this, MyJobService.class);
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setRequiresDeviceIdle(false)
.setRequiresCharging(false)
.setPeriodic(3000)
//.setOverrideDeadline(400) // Remove comment for faster testing.
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
int result = scheduler.schedule(jobInfo);
if (result == JobScheduler.RESULT_SUCCESS) {
Toast.makeText(this,"Start", Toast.LENGTH_LONG).show();
} } }
Inside MyJobService I through a Broadcast to my Activity, to let it know it has to update the content of the RecylcerView. 在MyJobService内部,我通过广播到我的Activity,以使其必须更新RecylcerView的内容。 The responsable of receiving the broadcast is an inner class like this:
负责接收广播的是一个内部类,如下所示:
public static class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
DataSource dataSource = new DataSource(context);
items = dataSource.getItems(); // can't do this due to outter class variable
adapter.refresh(ítems); // can't do this due to outter class variable
Toast.makeText(context,"event",Toast.LENGTH_SHORT).show();
}
}
Problem is on my main Activity I hold two private variable called items (which is the ArrayList ) and adapter ( which is the adapter of the recylcerview) I pass items to the adapter to update the recyclerView content.However, as I am in a static inner class I can't access outter class variables. 问题出在我的主Activity上,我拥有两个私有变量,分别称为items(即ArrayList)和adapter(即recylcerview的适配器),我将项目传递给适配器以更新recyclerView内容。内部类我无法访问外部类变量。 Whats the correct way to do something like this?
做这样的事情的正确方法是什么? I think I am messing too much and I guess there must be an easiest and more straight forward way to accomplish what I want.
我想我太混乱了,我想必须有一种最简单,更直接的方法来完成我想要的事情。
Thank you very much 非常感谢你
Well, turns out I was complicating things too much. 好吧,原来我把事情弄得太复杂了。 It can be done with a simple Handler like this:
可以使用像这样的简单Handler来完成:
// Create the Handler object (on the main thread by default)
Handler handler = new Handler();
// Define the code block to be executed
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Toast.makeText(getApplicationContext(),"Update",Toast.LENGTH_SHORT).show();
ItemsDS itemsDS = new ItempsDS(getApplicationContext());
items = itemsDS.getItems();
adapter.clear();
adapter.addAll(items);
handler.postDelayed(runnableCode, 1000);
}
};
There are Two ways to do this thing: 有两种方法可以执行此操作:
First is as mentioned by @akshayBhat, you can do it by removing static keyword from BroadcastReceiver class: 首先是@akshayBhat提到的,您可以通过从BroadcastReceiver类中删除static关键字来实现:
ArrayList<String> items;
Adapter adapter;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
DataSource dataSource = new DataSource(context);
items = dataSource.getItems(); // can't do this due to outter class variable
adapter.refresh(ítems); // can't do this due to outter class variable
Toast.makeText(context, "event", Toast.LENGTH_SHORT).show();
}
}
Second is to pass the reference of current activity state to the BroadCast Receiver and access ClassVariables there using that refernce; 其次是将当前活动状态的引用传递给BroadCast接收器,并使用该引用访问那里的ClassVariables。
ArrayList<String> items;
Adapter adapter;
public static class MyReceiver extends BroadcastReceiver {
AboutUs mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = (AboutUs)context;
DataSource dataSource = new DataSource(context);
mContext.items = dataSource.getItems(); // can't do this due to outter class variable
mContext.adapter.refresh(ítems); // can't do this due to outter class variable
Toast.makeText(context, "event", Toast.LENGTH_SHORT).show();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.