gpt4 book ai didi

java - 通知数据集更改后未调用 getview

转载 作者:行者123 更新时间:2023-12-02 11:06:11 30 4
gpt4 key购买 nike

所以我正在开发实时 ListView ,我需要 ListView 在单击按钮时更新,这些也是如此:

private void ReloadData(){
DataList.clear();
try{
for (int i =0;i<ItemsCount;i++){
TempItem=new STRUCTS.STRUCT_ProductItem();
TempItem.setName(dbh.ReadDefine(ProductID,i+"","name")+"");//Read name if there is any set
TempItem.setImgs(Uri.parse(dbh.ReadDefine(ProductID,i+"","img")+""));//Read image if there is any set
TempItem.setInfo("PR"+i);//Desc

Log.i("TAG", "ReloadData: "+IsOn[i]);//Is product avalible

TempItem.setToggled(IsOn[i]);
DataList.add(TempItem);

}
}catch (Exception ex){
ex.printStackTrace();
Log.e("ERRORCATCHER", "OnForLoop (Fill Product Data): "+ex.getMessage() );
}
//


if(!IsAdapterSet){

adapter = new LISTCUSTOM_ProductITEM(MyActivity.this,DataList);
list.setAdapter(adapter);


IsAdapterSet=true;
}else{


try {


adapter.swapItems(DataList);


} catch (Exception e) {
e.printStackTrace();
}

}


}

我的自定义适配器上的 SwapItems:

 public void swapItems(ArrayList<STRUCTS.STRUCT_ProductItem> list) throws InterruptedException {
DataList=list;
this.notifyDataSetChanged();

}

这没有给出任何错误,正如我从日志中看到的那样,新项目已成功添加,并且它们的数据是新的

listview 不会自行更新!所以当我单击运行 ReloadData() 的按钮时它对 ListView 没有影响...

实际上此日志正在运行:

      @Override
public View getView(final int CPosition, @Nullable View convertView, @NonNull ViewGroup parent) {
final STRUCTS.STRUCT_ProductItem Currect_Item=DataList.get(CPosition);
Log.i("TAG", "View updated : "+Currect_Item.gettoggled());
/*Define stuff*/
itempicture.setImageURI(Currect_Item.getImgs());
/*More listview update stuff*/

当我单击该按钮时,它不会记录 ViewUpdated,并且不会更改任何 ListView 元素

但是当我滚动 ListView 时,项目不可见,然后我向后滚动,它们的数据已更新!

GetView is runned when a item lose visibility then gain it again

为什么 Notifydatachanged 不会更新 UI/在我的自定义适配器上运行 GetView?

最佳答案

尝试编码

     private void ReloadData(){
DataList.clear();
if(!IsAdapterSet){

adapter = new LISTCUSTOM_ProductITEM(MyActivity.this,DataList);
list.setAdapter(adapter);


IsAdapterSet=true;
}
try{
for (int i =0;i<ItemsCount;i++){
TempItem=new STRUCTS.STRUCT_ProductItem();
TempItem.setName(dbh.ReadDefine(ProductID,i+"","name")+"");//Read name if there is any set
TempItem.setImgs(Uri.parse(dbh.ReadDefine(ProductID,i+"","img")+""));//Read image if there is any set
TempItem.setInfo("PR"+i);//Desc

Log.i("TAG", "ReloadData: "+IsOn[i]);//Is product avalible

TempItem.setToggled(IsOn[i]);
DataList.add(TempItem);

}
adapter.swapItems(DataList);
}catch (Exception ex){
ex.printStackTrace();
Log.e("ERRORCATCHER", "OnForLoop (Fill Product Data): "+ex.getMessage() );
}


}
}

在适配器类

public void swapItems(ArrayList<STRUCTS.STRUCT_ProductItem> list) throws InterruptedException {
DataList.addAll(list);
this.notifyDataSetChanged();

}

关于java - 通知数据集更改后未调用 getview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50941248/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com