android listview 滚动后显示错误数据(自定义适配器)

2023-11-21

我有一个奇怪的问题让我发疯。在我的 android 应用程序中,我定制了自己的从 ArrayAdapter 扩展的适配器。我添加适配器的 ListView 的项目可以是标记文本(不可编辑)、可编辑文本或微调器。 疯狂的是:当我滚动 ListView 时,出现两个问题:

(1) (选定的)value显示在微调器项目中changes有时虽然我只是滚动!当我单击微调器时,仍显示旧的选定值(应该由微调器显示的值) (2)orderListView 项目的数量changes当我滚动时!

=>但是数据在适配器没有改变(既不是数据本身,也不是顺序)-所以这一定是视图本身的问题?!也许android在后台缓存并且没有足够快地刷新ListViewItems或者类似的东西?!

有人可以帮我吗?

多谢!

好吧,我找到了一个不太好的解决方案,但它有效。我只是不再使用convertView,尽管这在内存和性能方面并不是最理想的。就我而言,应该没问题,因为我的 ListView 的最大项目数是 15。 这是我的适配器类:

 public class FinAdapter extends ArrayAdapter<Param>{
    public Param[] params;
    private boolean merkzettel;
    protected EditText pv;

    public FinAdapter(Context context, int textViewResourceId, Param[] items, boolean merkzettel) {
        super(context, textViewResourceId, items);
        this.params = items;
        this.merkzettel = merkzettel;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Param p = params[position];
        if(p != null){
            if(merkzettel){
                if (convertView == null) {
                    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = vi.inflate(R.layout.merk_det_item, null);
                }
                TextView tvl = (TextView) convertView.findViewById(R.id.paramM);
                TextView edl = (TextView) convertView.findViewById(R.id.param_valueM);
                TextView pal = (TextView) convertView.findViewById(R.id.param_unitM);
                if (tvl != null) {
                    tvl.setText(p.getName());                            
                }
                if(pal != null){
                    pal.setText(p.getUnit());
                }
                if(edl != null){
                    edl.setText(p.getDefData());
                }
            }
            else{
                if(p.isSelect()){

                if (convertView == null) {
                    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = vi.inflate(R.layout.fin_prod_list_item_select, null);
                }            
                TextView tvs = (TextView) convertView.findViewById(R.id.paramS);
                Spinner sp = (Spinner) convertView.findViewById(R.id.spinner_kalk);
                TextView paU = (TextView) convertView.findViewById(R.id.param_unitS);


                if (tvs != null) {
                    tvs.setText(p.getName());                            
                }
                if(paU != null){
                    paU.setText(p.getUnit());
                }
                if(sp != null){
                    String[] values = new String[p.getData().size()];
                    for(int i=0; i<values.length; i++){
                        values[i] = p.getData().get(i);
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_spinner_item, values);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    sp.setAdapter(adapter);
                    sp.setSelection(p.getData().indexOf(p.getDefData()));
                    sp.setOnItemSelectedListener(new OnItemSelectedListener(){
                        public void onItemSelected(AdapterView<?> parent,
                                View convertView, int pos, long id) {
                            p.setDefData(p.getData().get(pos));
                            p.setChanged(true);
                        }
                        public void onNothingSelected(AdapterView<?> arg0) {
                            // TODO Auto-generated method stub

                        }

                    });
                }
            }       
            else if(p.isEdit()){
                if (convertView == null) {
                    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = vi.inflate(R.layout.fin_prod_list_item_edit, null);
                }
                TextView pa = (TextView) convertView.findViewById(R.id.param);
                pv = (EditText) convertView.findViewById(R.id.param_value);
                TextView paE = (TextView) convertView.findViewById(R.id.param_unit);
                if (pa != null) {
                    pa.setText(p.getName());                            
                }
                if(paE != null){
                    paE.setText(p.getUnit());
                }
                if(pv != null){
                    pv.setText(p.getDefData());
                    pv.setOnEditorActionListener(new OnEditorActionListener(){
                        public boolean onEditorAction(TextView convertView, int actionId,
                                KeyEvent event) {
                            // TODO Auto-generated method stub
                            p.setDefData(pv.getText().toString());
                            p.setChanged(true);
                            return false;
                        }                   
                    }); 
                }

            }
            else if(p.isLabel()){
                if (convertView == null) {
                    LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = vi.inflate(R.layout.fin_prod_list_item_label, null);
                }
                TextView tvl = (TextView) convertView.findViewById(R.id.paramL);
                TextView edl = (TextView) convertView.findViewById(R.id.param_valueL);
                TextView pal = (TextView) convertView.findViewById(R.id.param_unitL);
                if (tvl != null) {
                    tvl.setText(p.getName());                            
                }
                if(pal != null){
                    pal.setText(p.getUnit());
                }
                if(edl != null){
                    edl.setText(p.getDefData());
                }   
            }}
        }
        return convertView;
    }
}

我在列表中的多个项目类型上遇到了类似的问题。就我而言,列表项是部分(标签)项或公共列表项。

要使用此类列表,您应该覆盖getViewTypeCount and getItemViewType方法。 像这样的东西:

private static final int ITEM_VIEW_TYPE_ITEM = 0;
private static final int ITEM_VIEW_TYPE_SEPARATOR = 1;

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    return this.getItem(position).isSection() ? ITEM_VIEW_TYPE_SEPARATOR : ITEM_VIEW_TYPE_ITEM;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Item item = this.getItem(position);

    if (convertView == null) {
        convertView = mInflater.inflate(item.isSection() ? R.view1 : R.view2, null);
    }

    if(item.isSection()){
        //...
    }
    else{
        //...
    }

    return convertView;
}

那么convertView参数将始终是正确的并包含您需要的类型。

另一件事:你明确添加了public Param[] params当您已经在基类中拥有该字段时ArrayAdapter<Param>.

我建议继承BaseAdapter class.

Edit:这是您可以尝试使用的代码,以便使您的Spinner work:

sp.setTag(p);
sp.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View convertView, int pos, long id) {
    Param currentItem = (Param)parent.getTag();
    currentItem.setDefData(currentItem.getData().get(pos));
    currentItem.setChanged(true);
}
//...

尝试使用以下组合getTag and setTag方法。我记得我在事件处理程序和最终变量方面遇到了类似的问题,但我完全忘记了它们的原因,所以我无法准确解释为什么会发生这种情况。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android listview 滚动后显示错误数据(自定义适配器) 的相关文章

随机推荐

  • 如何使用 jquery 隐藏父元素?

    假设以下 HTML li class fooli a class foo href anchor a li li class fooli a class foo href anchor a li 和以下 Javascript 使用 jque
  • ASP.NET 的 JSON 最大长度问题

    我正在创建一个 asp net 2 0 Web 服务 它提供 json 作为输出 并且有一个非常大的 无法分解的数据集 超出了最大长度限制 我在互联网上搜索过 net 3 5 4 上有解决方案 但 2 0 上没有 谁能告诉我如何增加 JSO
  • Android Gradle Jacoco:用于集成测试的离线工具

    我们正在构建一个 Android 应用程序 并使用 Appium 进行测试 现在我想看看我们的 Appium 测试的测试覆盖率 我认为这是可能的 因为Jacoco支持离线检测 http www eclemma org jacoco trun
  • 如何在 Visual Studio Code 中以 noprofile 启动 Powershell 脚本

    如何在 Visual Studio Code 中以 noprofile 启动 Powershell 脚本 我可以使用 noprofile with 命令运行 Powershell IsePowerShell Ise NoProfile 但是
  • 厨师包装食谱最佳实践

    在学习厨师的过程中 我看到了包装食谱的相互冲突的模式 例如 一些食谱使用default rb 而另一些则使用customize rb进行覆盖 attributes default rb attributes customize rb 哪个是
  • (仍然)无法正确安装 python 的 lxml 2.3,但至少 2.2.8 可以工作

    2011年6月30日 我因为这个问题而奖励 Pablo 因为他answer 由于他的评论中讨论的原因 我仍然无法正确安装 lxml 2 3 我收集了一些可以做的工作 但我已经在这个问题上花费了大量的时间 不过 我已经编写了所需的代码并成功安
  • selenium webdriver 找到锚标记并单击它

    div ul class selectors modeSelectors li a href content l411846326l1213g references title span class selector References
  • 使用 lambda 作为参数:std::function 还是模板?

    我正在学习 c 11 对 lambda 特别感兴趣 经过一些实践 我假设 lambda 闭包是一个无名函数对象 所以我写了这段代码 template
  • 基于“Accept”标头的网页有条件服务?

    据我所知 您可以通过以下方式通过 Express 提供静态内容 app use express static dirname public html 但是 我试图根据响应发送的 Accept 标头来明确更改其提供的内容的呈现方式 通常 我拥
  • 使用 Rails 运行多个后台并行作业

    在我的 Ruby on Rails 应用程序中 我需要并行执行 50 个后台作业 每个作业都会创建到不同服务器的 TCP 连接 获取一些数据并更新活动记录对象 我知道执行此任务的不同解决方案 但其中任何一个都是并行的 例如 delayed
  • UTF-8 字符的 Html2canvas 图像捕获问题

    我想捕获我的网页 为此我找到了 html2canvas 当我如下所示使用时 我的 UTF 8 波斯语 字符遇到麻烦 并且这个方向被破坏 如您所见 HTML div span span div JavaScript document read
  • 如何让 kestrel Web 服务器监听非本地主机请求?

    我已将我的 c asp net 5 mvc 6 应用程序部署到 Windows 2008 服务器 我已经火力全开dnx web它正在侦听端口 5000 并且从本地计算机访问时工作正常 如何让它监听非本地主机请求 附 这question与此不
  • 如何实现一个工厂有多个交换机?

    我想实现一个工厂函数来创建对象 我的对象模板如下所示 template
  • 如何在 C 中实现循环缓冲区?

    我需要一个固定大小 在创建时可以在运行时选择 而不是编译时选择 的循环缓冲区 它可以容纳任何类型的对象 并且需要very高性能 我认为不会出现资源争用问题 因为尽管它处于多任务嵌入式环境中 但它是一个合作环境 因此任务本身可以管理它 我最初
  • XSLT:循环一次选择两个元素

    我有一堆 xml 文档 作者选择在其中表示一组笛卡尔点 如下所示
  • 如何读取正在使用的文件? [复制]

    这个问题在这里已经有答案了 我有一个小问题 我有一个应该每天解析日志文件的工具 不幸的是这个日志文件正在被写入日志的进程使用 我无法阻止它 第一次尝试是创建该文件的副本 但这也不起作用 有什么方法可以让我读取日志文件的当前文本 即使它已经在
  • RPM 有条件 可能需要在规格文件中

    正如主题所读 我想知道是否可以在 RPM 规范文件中使所需的包依赖于某个条件 例如 如果在安装目标上 则检查 shell 语句 例如主机正在使用绑定接口 并且仅当要求生效时 作为您最初问题的答案 是的 这是可能的 但是您可以实现的内容取决于
  • 设置图像最大尺寸

    我需要设置图像的最大高度和宽度 img 标签 例如 最大尺寸为 400x400 px 因此如果图像尺寸小于此尺寸 则它将按原样显示图像 如果尺寸大于此尺寸 则应将其压缩到此尺寸 我怎样才能在 html 或 javascript 中做到这一点
  • 在 rake 任务中打印到屏幕

    我有一个长时间运行的 rake 任务 我时不时地在屏幕上打印更新 让我知道任务已经完成了多少 puts Almost there 我的问题是所有的 put 语句似乎都缓冲在某处 并且在任务完成之前不会打印到屏幕上 此时 它们将被一次性打印出
  • android listview 滚动后显示错误数据(自定义适配器)

    我有一个奇怪的问题让我发疯 在我的 android 应用程序中 我定制了自己的从 ArrayAdapter 扩展的适配器 我添加适配器的 ListView 的项目可以是标记文本 不可编辑 可编辑文本或微调器 疯狂的是 当我滚动 ListVi