Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android ListView适配器调用notifyDataSetChanged时出错,Android错误?_Java_Android_Android Listview_Baseadapter_Notifydatasetchanged - Fatal编程技术网

Java Android ListView适配器调用notifyDataSetChanged时出错,Android错误?

Java Android ListView适配器调用notifyDataSetChanged时出错,Android错误?,java,android,android-listview,baseadapter,notifydatasetchanged,Java,Android,Android Listview,Baseadapter,Notifydatasetchanged,在我一直在开发的应用程序中,我有一个自定义类DeviceListAdapter扩展BaseAdapter,它被传递到我的ListView。在我的DeviceListAdapter类中,我保留了自己的ArrayList,我使用View getView(…)生成列表视图。每当应用程序导致数据发生更改时,我都会使用DeviceListAdapter中的自定义方法更新ArrayList以反映更改。我使用了调试器和许多print语句来检查数据是否按预期方式更改,并按指定添加和删除Device对象。但是,每

在我一直在开发的应用程序中,我有一个自定义类
DeviceListAdapter
扩展
BaseAdapter
,它被传递到我的
ListView
。在我的
DeviceListAdapter
类中,我保留了自己的
ArrayList
,我使用
View getView(…)
生成列表视图。每当应用程序导致数据发生更改时,我都会使用
DeviceListAdapter
中的自定义方法更新
ArrayList
以反映更改。我使用了调试器和许多print语句来检查数据是否按预期方式更改,并按指定添加和删除
Device
对象。但是,每次更改数据后,我也会调用
notifyDataSetChanged()
,但在UI上没有任何元素得到更新。在调试器中,我发现在调用
notifyDataSetChanged()
之后,没有调用
getView(…)
方法,这解释了为什么没有重新绘制
ListView
。为了找出原因,我使用调试器的“单步执行”函数跟踪程序执行进入android框架的位置,因为我下载了SDK源代码。我的发现很有趣。执行的路径是这样的:

public View getView(final int position, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null) //Regenerate the view
    {
        /* Instantiate your view */
    } else {
        view = convertView;
    }
    // populate the elements in view like EditText, TextView, ImageView and etc
    return view;
}
DeviceListAdapter.notifyDataSetChanged()
BaseAdapter.notifyDataSetChanged()
DataSetObservable.notifyChanged()
AblistView.onInvalidated()

它不是调用
onChanged()
方法,而是在到达
AbsListView
时跳过跟踪并执行
onInvalidated()
方法。起初我认为这是调试器读取错误行号的错误,但我重新启动了Android Studio,并完全卸载和重新安装了应用程序,但结果是一样的。有谁能告诉我这是否是Android框架的一个合理问题,或者调试器在跟踪我自己的项目文件之外的执行时是否不可靠

更多关于我的
notifyDataSetChanged()
实现的信息。。。我创建了本地方法来覆盖
BaseAdapter
notifyDataSetChanged()
,这样我就可以在我的
DeviceListAdapter
内部设置一个布尔标志
mForceRedraw
,以决定是否应该强制重画我的列表条目。在
getView(…)
方法中,我通常检查第二个参数
View convertView
是否为null,如果为null,则我重新绘制视图,如果不是,则传递convertView并返回它。但是,当“mForceRedraw”为true时,我从不返回
convertView
,而是显式地重新绘制视图。出现的问题是由我前面的问题引起的,即在我执行
notifyDataSetChanged()
后,没有调用
getView()

编辑:以下是我的
DeviceListAdapter
的代码片段:

/**
*向mDeviceListView提供有关当前设备数据的数据。管理动态和
*已配置设备的持久存储,并构造每个设备的视图
*用于在列表中放置的列表项。
*/
私有类DeviceListAdapter扩展BaseAdapter{
私有布尔值mForceRedraw=false;
/**
*动态阵列,用于跟踪当前正在管理的所有设备。
*它保存在内存中,并且易于访问,以便系统调用
*可以快速满足视图更新请求。
*/
私有列表mDeviceEntries;
私有上下文;
公共设备适配器(上下文){
this.mContext=上下文;
this.mDeviceEntries=新的ArrayList();
populateFromStorage();
}
/**
*将给定设备插入存储器,并通知mDeviceListView数据更新。
*@param newDevice要添加到内存中的设备。
*/
公开作废认沽权(装置新装置){
先决条件。checkNotNull(newDevice);
布尔FlagUpdateExisting=false;
用于(设备:mDeviceEntries){
if(newDevice.isVersionOf(设备)){
int index=mDeviceEntries.indexOf(设备);
如果(索引!=-1){
mDeviceEntries.set(索引,新设备);
FlagUpdateExisting=true;
打破
}否则{
抛出新的非法状态异常();
}
}
//如果现有设备未更新,则这是新设备,请将其添加到列表中
如果(!FlagUpdateExisting){
mDeviceEntries.add(新设备);
}
TECDataAdapter.setDevices(mDeviceEntries);
notifyDataSetChanged();
}
/**
*如果给定设备存在于存储器中,请将其删除并从mDeviceListView中删除。
*@param设备
*/
公共无效删除(设备){
先决条件。checkNotNull(设备);
//从mDeviceEntries中删除设备
迭代器迭代器=mDeviceEntries.Iterator();
while(iterator.hasNext()){
设备d=(设备)迭代器.next();
if(设备的isVersionOf(d)){
iterator.remove();
}
}
TECDataAdapter.setDevices(mDeviceEntries);
notifyDataSetChanged();
}
/**
*从持久性存储中检索设备条目并将其加载到动态存储中
*负责在listView中显示条目的数组。
*/
公共无效populateFromStorage(){
List temp=premissions.checkNotNull(tecdatadapter.getDevices());
mDeviceEntries=临时;
notifyDataSetChanged();
}
public int getCount(){
public View getView(final int position, View convertView, ViewGroup parent) {
            LinearLayout view;
            if (convertView == null) //Regenerate the view
            {

              /* inflate Draws my views */

            } else 
            {
                view = (LinearLayout) convertView;

            }

            //repopulate this view with the data that needs to appear at this position using getItem(position)


            return view;
        }