有时listView.getChildAt(int index)返回NULL(安卓)getChildAt、listView、int、NULL

2023-09-13 01:59:18 作者:久病成医i

我有一个自定义适配器的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...

让我告诉你的code:

Let me show you the code:

        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()

这是怎么回事?

详细数据:在code是AnimationListener听众的onAnimationStart(动画动画)内

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

推荐答案

由于认为回收, listView.getChildAt()只返回的位置,有一个观点显示,也许有更多。也许,如果你分享你更多的code可以帮助你找出如何最好地解决这个问题。

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.