我有一个带有Recycler视图处理卡的活动。我有一个SwipeRefreshLayout,所以当我滑动它时,用新内容更新RecyclerView。到现在为止还挺好。
Howerver,我想每隔X秒更新一次RecyclerView,所以如果我在打开recylcerview的情况下离开活动,我忘了刷它,它也会自行更新。 为此,我想到这样的事情: (我省略了必要的代码)。
包含recyclerview的主要活动安排了这样的Job:
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();
} } }
在MyJobService内部我通过广播到我的Activity,让它知道它必须更新RecylcerView的内容。 接收广播的责任是这样的内部类:
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();
}
}
问题在于我的主要活动我持有两个名为items的私有变量(即ArrayList)和适配器(它是recylcerview的适配器)我将项目传递给适配器以更新recyclerView内容。但是,因为我是在静态内部类中,我无法访问outter类变量。 做正确的方法是什么?我想我太乱了,我想必须有一种最简单,更直接的方法来实现我的目标。
非常感谢
答案 0 :(得分:1)
好吧,事实证明我太复杂了。它可以通过这样的简单处理程序来完成:
// 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);
}
};
答案 1 :(得分:0)
有两种方法可以做到这一点:
首先如@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();
}
}
其次是将当前活动状态的引用传递给BroadCast Receiver并使用该引用访问那里的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();
}
}