迭代列表视图项并修改子视图

发布于 2024-11-17 23:49:18 字数 2968 浏览 1 评论 0原文

listView.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View viewItem, int position, long arg3) {


        for(int i =parent.getFirstVisiblePosition(); i <parent.getLastVisiblePosition(); i ++)
        {
            Log.i("in", Integer.toString(i));
            View v = parent.getChildAt(i);

            ((Button)v.findViewById(R.id.removefav)).setVisibility(View.INVISIBLE);
            ((Button)v.findViewById(R.id.gotherefav)).setVisibility(View.INVISIBLE);


        }
        viewItem=listView.getChildAt(position);
        ((Button)viewItem.findViewById(R.id.removefav)).setVisibility(View.VISIBLE);
        ((Button)viewItem.findViewById(R.id.gotherefav)).setVisibility(View.VISIBLE);
        TextView text = (TextView)viewItem.findViewById(R.id.item);
        name = text.getText();

如果您单击 ListView 第一次启动时可见的项目,则此代码有效,但如果您滚动并拥有(假设有一半可见的项目),则会抛出 NullpointerException

07-02 18:38:47.290: WARN/dalvikvm(6078): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-02 18:38:47.290: ERROR/AndroidRuntime(6078): Uncaught handler: thread main exiting due to uncaught exception
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): java.lang.NullPointerException
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at walk.me.Tabber$1.onItemClick(Tabber.java:89)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.widget.ListView.performItemClick(ListView.java:3285)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.os.Handler.handleCallback(Handler.java:587)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.os.Looper.loop(Looper.java:123)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at java.lang.reflect.Method.invoke(Method.java:521)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at dalvik.system.NativeStart.main(Native Method)

我想做的是,我有一个 ListView 和两个按钮。用户触摸子视图(两个按钮)显示的项目之一,当用户触摸另一个项目时,先前选择的项目上的按钮消失。

我做错了什么?那个“半可见”项目(当列表滚动时)是否会导致 NPE?我也尝试从那里开始,

i=0

但结果是一样的。

listView.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View viewItem, int position, long arg3) {


        for(int i =parent.getFirstVisiblePosition(); i <parent.getLastVisiblePosition(); i ++)
        {
            Log.i("in", Integer.toString(i));
            View v = parent.getChildAt(i);

            ((Button)v.findViewById(R.id.removefav)).setVisibility(View.INVISIBLE);
            ((Button)v.findViewById(R.id.gotherefav)).setVisibility(View.INVISIBLE);


        }
        viewItem=listView.getChildAt(position);
        ((Button)viewItem.findViewById(R.id.removefav)).setVisibility(View.VISIBLE);
        ((Button)viewItem.findViewById(R.id.gotherefav)).setVisibility(View.VISIBLE);
        TextView text = (TextView)viewItem.findViewById(R.id.item);
        name = text.getText();

This code works if you are clicking on items that are visible on the first start of the ListView, but if you scroll and have, lets say half visible item, throws NullpointerException:

07-02 18:38:47.290: WARN/dalvikvm(6078): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-02 18:38:47.290: ERROR/AndroidRuntime(6078): Uncaught handler: thread main exiting due to uncaught exception
07-02 18:38:47.300: ERROR/AndroidRuntime(6078): java.lang.NullPointerException
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at walk.me.Tabber$1.onItemClick(Tabber.java:89)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.widget.ListView.performItemClick(ListView.java:3285)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.os.Handler.handleCallback(Handler.java:587)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.os.Looper.loop(Looper.java:123)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at java.lang.reflect.Method.invoke(Method.java:521)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-02 18:38:47.300: ERROR/AndroidRuntime(6078):     at dalvik.system.NativeStart.main(Native Method)

What I'm trying to do is, I have a ListView and two buttons. And the user touches one of the items the child views (two buttons) show up, when the user touches another item, buttons on previously selected item disappear.

What I'm doing wrong? Is that "half-visible" item (when the list is scrolled) makes the NPE? I also tried starting from

i=0

but it was the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪若未夕 2024-11-24 23:49:18

您不应该像 listView 中那样修改视图。 android 中的列表视图始终由适配器支持,并且经过优化,以便在滚动出屏幕时重新使用视图。这意味着您始终应该在适配器中实现 getView() ,以反映数据(适配器中的模型)如何在视图中呈现。
当您收到要在 getView 参数中回收的视图时,正确反应非常重要(即不要从头开始创建它,而是根据与视图对应的适配器中的项目设置适当的字段、文本、可见性等) 。

每当您修改适配器中的所有相关数据(更改指示您的情况的可见性的标志)时,您应该在适配器上调用notifyDataSetChanged(),然后列表视图将重新创建自己的所有可见项目(它将循环遍历它们并调用 getView() 重用已创建的视图

另请参阅 Android listActivity onListItemClick with CheckBox 涵盖了类似的问题。

You should not modify the views like that in listView. The list view in android is always backed by an Adapter and it is optimised so that views are re-used when they are scrolled out of the screen. This means that you always should have getView() implemented in adapter that should reflect how the data (model in adapter) is presented in the view.
It is important to react properly when you receive a view to recycle in getView parameters (i.e. not to create it from scratch, but rather than that set proper fields, texts, visibility etc. according to the item from the adapter corresponding to the view).

Whenever you modify all the relevant data in the adapter (change a flag indicating the visibility in your case) you should call notifyDataSetChanged() on the adapter and then the listview will recreate all the visible items on its own (it will loop through them and call getView() reusing the views that were already created.

Look also at Android listActivity onListItemClick with CheckBox which covers similar problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文