Android 当自定义适配器的数据依赖于另一个类时,如何自动刷新该适配器的内容?
当数据在另一个类中更改时,我试图使用自定义适配器上的Android 当自定义适配器的数据依赖于另一个类时,如何自动刷新该适配器的内容?,android,listadapter,android-adapterview,Android,Listadapter,Android Adapterview,当数据在另一个类中更改时,我试图使用自定义适配器上的notifyDataSetChanged()从一个类更改视图 Class FragmentOne在添加列表项的位置启动线程 然后我使用classfragmenttwo获取那些列表项并更新我的视图 我的代码--- 课程片段一 static ArrayList<String> listItems = null; private class MonitorLogThread extends Thread { BufferedRe
notifyDataSetChanged()
从一个类更改视图
Class FragmentOne
在添加列表项的位置启动线程
然后我使用classfragmenttwo
获取那些列表项
并更新我的视图
我的代码---
课程片段一
static ArrayList<String> listItems = null;
private class MonitorLogThread extends Thread
{
BufferedReader br;
@Override
public void run() {
while(((line=br.readLine()) != null) && !this.isInterrupted()){
listItems.add(line);
}
}
静态ArrayList listItems=null;
私有类MonitorLogThread扩展线程
{
缓冲剂;
@凌驾
公开募捐{
而(((line=br.readLine())!=null)和&!this.isInterrupted()){
添加(行);
}
}
课堂片段二
private BBLogListAdapter mBBLogListAdapter;
ArrayList<String> receiptlist;
@Override
protected void onResume() {
super.onResume();
if(receiptlist == null)
receiptlist = new ArrayList<String>();
else
receiptlist.clear();
// Initializes list view adapter.
mBBLogListAdapter = new BBLogListAdapter();
setListAdapter(mBBLogListAdapter);
mBBLogListAdapter.updateReceiptsList(FragmentOne.listItems);
mBBLogListAdapter.notifyDataSetChanged();
}
// Custom Adapter
private class BBLogListAdapter extends BaseAdapter {
//Code to set view dynamically according to data in adapter
public void updateReceiptsList(ArrayList<String> newlist) {
receiptlist.clear();
receiptlist.addAll(newlist);
}
//Some more code..
}
}
私有BBLogListAdapter mBBLogListAdapter;
ArrayList接收列表;
@凌驾
受保护的void onResume(){
super.onResume();
if(receiptlist==null)
receiptlist=新的ArrayList();
其他的
receiptlist.clear();
//初始化列表视图适配器。
mBBLogListAdapter=新BBLogListAdapter();
setListAdapter(mBBLogListAdapter);
mBBLogListAdapter.updateReceiptsList(FragmentOne.listItems);
mBBLogListAdapter.notifyDataSetChanged();
}
//自定义适配器
私有类BBLogListAdapter扩展了BaseAdapter{
//根据适配器中的数据动态设置视图的代码
公共无效更新接收列表(ArrayList newlist){
receiptlist.clear();
receiptlist.addAll(newlist);
}
//还有一些代码。。
}
}
因此,目前我正在onResume()方法中使用updateReceiptsList方法来更新
目录这仅在我们恢复活动时更新
但是我想根据另一个活动的数据更改,在此活动中自动刷新内容。抓住适配器对象,在“FragmentTwo”中创建一个静态方法
当线程中的数据发生更改时,调用此静态方法
static ArrayList<String> listItems = null;
private class MonitorLogThread extends Thread
{
BufferedReader br;
@Override
public void run() {
while(((line=br.readLine()) != null) && !this.isInterrupted()){
listItems.add(line);
FragmentTwo.notifyDataChanged();
}
}
PS:一个更干净的选择是在“Fragment2”上使用侦听器,侦听“FragmentOne”上的任何数据更改。有关实施细节,请参见此处-
希望这有帮助。解决此类问题的正确方法是使用ContentProvider
s和CursorAdapter
s。当数据发生更改时,您的适配器将收到通知,所有同步都将由框架本身处理。痛苦越少,收获越大。当数据发生更改时,在mBBLogListAdapter对象上调用notifyDataSetChanged不起作用吗?每当我恢复活动时,它都起作用,因为我在onResume()上调用它。
。我在活动中的任何时候都可以用它来称呼它?我是否需要广播接收器来通知更改然后更新?这将导致对非线程安全对象的并发访问。这也会导致IllegalStateExceptions.mBBLogListAdapter在我调用notifyDataChanged时为null。因为更新发生在不同的线程上,所以在调用notifyDataSetChanged之前,您应该添加null检查以确保适配器已经初始化。当FragmentOne中的数据发生更改时,我将编辑以添加此空检查。在Fragment2上使用事件侦听器处理事件不会也起作用吗?这样做会给片段引入太多的状态依赖性。你必须知道你的片段、活动和任何上下文都可以随时被Android操作系统关闭。你不能仅仅假设UI组件会出现在那里。正因为如此,若你们打算在UI上使用任何数据,你们都必须持久化。我根本不和数据库交互。我从某个进程命令获取数据,然后调用notifyDataSetChanged。那么为什么要使用ContentProviders和CursorAdapter呢?我的意思是你应该这样做。您应该尽可能多地持久化数据。这不是要持久化到数据库中的数据。这些是不断变化的系统日志。我只想在notifyDataSetChanged的视图中显示最近的。
static ArrayList<String> listItems = null;
private class MonitorLogThread extends Thread
{
BufferedReader br;
@Override
public void run() {
while(((line=br.readLine()) != null) && !this.isInterrupted()){
listItems.add(line);
FragmentTwo.notifyDataChanged();
}
}
setListAdapter(mBBLogListAdapter);