有时 listView.getChildAt(int index) 返回 NULL (Android) [英] Sometimes listView.getChildAt(int index) returns NULL (Android)

查看:18
本文介绍了有时 listView.getChildAt(int index) 返回 NULL (Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义适配器的 listView.当发生某些事情时(在孩子中单击)我会做一些计算并修改子视图.如果某些条件已满足,则应修改与单击的子项无关的其他子项.

I have a listView with a custom adapter. When something happens (a click in a child) I do some calculation things and modify the child View. IF some condition has been fulfilled then other child unrelated to the clicked child should be modified.

这有时有效,但有时失败,DDMS 说视图为空...

This sometimes works, but sometimes fails and the DDMS says that the view is null...

让我向您展示代码:

        if(invalidaEste != -1)
        {
            try
            {
                View v = lv_data.getChildAt(invalidaEste);
                if( v== null)
                {
                    Log.e("MY_LOG", "SIZE " + lv_data.getCount());
                    Log.e("MY_LOG", "IS_NULL " + String.valueOf(invalidaEste)); 
                }

                if(invalidaEste >= lv_data.getFirstVisiblePosition() &&
                   invalidaEste <= lv_data.getLastVisiblePosition())
                {
                    RelacionFacturaPago rpf = (RelacionFacturaPago)lv_data.getAdapter().getItem(invalidaEste);
                    TextView tv = (TextView)v.findViewById(R.id.tv_pendiente);
                    tv.setText(Formato.double2Screen(rpf.getPorPagar()));
                }
            }
            catch (Exception e)
            {
                Log.e("MY_LOG", "FAIL");
                Log.e("MY_LOG", String.valueOf(invalidaEste));
            }

        }

invalidaEste 是我要修改的视图.当 v 为空 时,我会记录索引以检查它是否正常.始终小于或等于 listView.getCount()

invalidaEste is the view that I want to modify. When v is null I log the index to check if it is OK. Always is smaller or equal than the listView.getCount()

为什么会这样?

更多数据:代码位于 AnimationListener 侦听器的 onAnimationStart(Animation animation) 内.

More data: The code is inside of the onAnimationStart(Animation animation) of an AnimationListener listener.

推荐答案

由于视图回收,listView.getChildAt() 将只返回它正在显示的位置的视图,也许还有一个.也许如果您分享更多代码,我们可以帮助您找出解决问题的最佳方法.

Because of view recycling, listView.getChildAt() will only return a view for the positions it is displaying, and maybe one more. Maybe if you share more of your code we can help you figure out how to best tackle the problem.

这篇关于有时 listView.getChildAt(int index) 返回 NULL (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆