所以我正在实时列表视图中工作,我需要列表视图以在单击按钮时进行更新,所以这些操作也是这样:
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();
}
}
}
我的自定义适配器上的交换项:
public void swapItems(ArrayList<STRUCTS.STRUCT_ProductItem> list) throws InterruptedException {
DataList=list;
this.notifyDataSetChanged();
}
这没有给出任何错误,正如我从“日志”中看到的那样,新项目已成功添加并且其数据是新的
listview不会自我更新!所以当我单击运行ReloadData()的按钮时 它对列表视图没有影响...
该日志正在运行:
@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元素
但是,当我滚动列表视图时,因此看不到任何项,然后我向后滚动,其数据将更新!
当某项失去可见性然后再次获得它时,将运行GetView
为什么Notifydatachanged无法更新UI /在我的自定义适配器上运行GetView?
答案 0 :(得分:1)
尝试使用此代码
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();
}
答案 1 :(得分:0)
尝试一下
private void ReloadData(){
ArrayList<STRUCTS.STRUCT_ProductItem> list = new ArrayList<>;
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]);
list.add(TempItem);
}
} catch (Exception ex){
ex.printStackTrace();
Log.e("ERRORCATCHER", "OnForLoop (Fill Product Data): "+ex.getMessage() );
}
DataList.clear();
DataList.addAll(list);
adapter.notifyDataSetChanged();
}