文档开发者中心
我的

ListContainer方法notifyDataChanged实现动态改变数据使用注意事项

今天有坛友说ListContainer方法notifyDataChanged更新数据用不了,我看了一下其实不是用不了,而是可能遇到了坑,下面说一下原因

首先定义Provider类,代码如下

package com.example.phonetest;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class TemplateItemProvider extends BaseItemProvider {
    private List<TemplateItem> list;
    private AbilitySlice slice;

    public TemplateItemProvider(List<TemplateItem> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt=component;
        if(cpt==null)
        {
            cpt= LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_ItemTemplate,null,false);
        }
        TemplateItem item=list.get(i);
        Text text=(Text)cpt.findComponentById(ResourceTable.Id_item_Index);
        text.setText(item.getName());
        return cpt;
    }
}

然后定义模板类,代码如下

package com.example.phonetest;

public class TemplateItem {
    private String name;

    public TemplateItem(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

然后定义模板布局xml,代码如下

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:left_margin="16vp"
    ohos:right_margin="16vp"
    ohos:orientation="vertical">
    <Text
        ohos:id="$+id:item_Index"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:padding="4vp"
        ohos:text="item"
        ohos:text_size="20fp"
        ohos:layout_alignment="center"/>
</DirectionalLayout>


再然后是主页面布局xml,代码如下


<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:id="$+id:dlListContainer"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:orientation="vertical">
    <ListContainer
        ohos:id="$+id:studentInfos"
        ohos:height="100vp"
        ohos:width="300vp"
        >
    </ListContainer>
    <Button
        ohos:id="$+id:btnChange"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_size="30vp"
        ohos:text="改变数据"></Button>
</DirectionalLayout>

然后是后台代码

package com.example.phonetest.slice;

import com.example.phonetest.ResourceTable;
import com.example.phonetest.TemplateItem;
import com.example.phonetest.TemplateItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.database.DataSetSubscriber;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

public class ListContainerItemTestSlice extends AbilitySlice {
    static final HiLogLabel label=new HiLogLabel(HiLog.LOG_APP,0x0001,"ListContainer测试");
    List<TemplateItem> list=null;
    ListContainer container =null;
    TemplateItemProvider tempProvider=null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_list_container_item_test);
        initListContainer();
        Button btnChange=(Button)findComponentById(ResourceTable.Id_btnChange);
        btnChange.setClickedListener(c->{
              //在原有list上面作更改会更新
//            list.add(new TemplateItem("item11"));
//            TemplateItem tempItem=list.get(2);
//            tempItem.setName("item9");
//            list.remove(2);
//            list.clear();
            //重新获取list对象是不会更新的
            list=getData(11);
            tempProvider.notifyDataChanged();

        });
    }
    private void initListContainer()
    {
        container = (ListContainer) findComponentById(ResourceTable.Id_studentInfos);
        list=getData(0);
        tempProvider=new TemplateItemProvider(list,this);
        container.setItemProvider(tempProvider);
    }

    private ArrayList<TemplateItem> getData(int startIndex){
        ArrayList<TemplateItem> list=new ArrayList<>();
        for(int i=startIndex;i<=startIndex+8;i++)
        {
            list.add(new TemplateItem("item"+i));
        }
        return list;
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

重点来了,上面代码我定义了一个getData方法可以传递item起始设置,当我获取list并传递给container以后,我在按钮点击事件里作出以下操作

  1. 对list新增一个元素

  2. 对list中原有元素进行修改

  3. 对list中原有元素进行删除

  4. 清空list中所有元素


UI会自动更新,但是我如果list重新赋值一个新的对象则是没有反应的,UI不会自动更新。


最后总结下来就是对原有绑定的list进行的操作是有效的,UI会自动更新,大伙可以将代码中的注释打开试试

点赞
1
收藏
4
回复
8
分享
举报
浏览659 发布于2021-04-13 16:31未知归属地
全部评论
最多点赞
最新发布
最早发布

我在BaseItemProvider的子类MyProvider里定义一个如下方法,在AbilitySlice里面更新数据时,调用myProvider.upDateData(list),list为传入的新数据集,亲测有效。 

重点notifyDataChanged()方法需要放在主线程!

  public void upDateData(List<Device> list){ 

        deviceInfos=list; 

        context.getUITaskDispatcher().asyncDispatch(new Runnable() { 

            @Override 

            public void run() { 

                notifyDataChanged(); 

            } 

        }); 

    } 

5
3楼回复于2022-04-02 06:57 未知归属地
  • hlglsj
    老哥牛逼,我找了好几种方法,还是这个靠谱,能解释一下原理吗
    2022-04-10 11:31 未知归属地
  • VToucher
    之前试过直接修改list对象、重新set provider、mainslice里异步notifydatachanged都不行,这个成功了,遇到类似问题的可以试试
    2022-05-10 09:54 未知归属地
  • Max0821
    感谢分享!这个真的好用! Ps:想问一下,如果我想在刷新列表数据的同时改变iten的背景色该如何操作……
    2022-05-11 09:11 未知归属地
  • 展开剩余2条回复

list重新赋值一个新的对象则是没有反应的,UI不会自动更新。

——需要重新把这个list传给Provider然后再设置Provider才生效:

= TemplateItemProvider().setItemProvider()


2楼回复于2021-05-06 06:56 未知归属地

虽然不是很懂,但觉得很厉害

1楼回复于2021-04-14 02:12 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。