无法对listView的子进行操作

时间:2014-04-11 07:16:45

标签: android android-layout listview

我希望得到listview的子代,并将其两个隐藏元素的可见性设置为true。 我的代码是

canvasListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                /*
                joinCanvasBtn.setEnabled(true);
                joinCanvasPasswordTxt.setEnabled(true);*/
                Log.d(TAG,"Selected positon : "+position);
                int index = canvasListView.getFirstVisiblePosition() + position;
                View v = canvasListView.getChildAt(index);
                if(v!=null) {
                    joinCanvasBtn = (Button) v.findViewById(R.id.joinCanvasBtn);
                    canvasPassword = (EditText) v.findViewById(R.id.joinCanvasPasswordTxt);

                    joinCanvasBtn.setVisibility(View.VISIBLE);
                    joinCanvasPasswordTxt.setVisibility(View.VISIBLE);

                }
                else {
                    Log.d(TAG,"Unable to find the selected child of listView");
                }


            }
        });

但这不起作用它给我Nullpointer例外。我的布局文件如下。基本上我想在用户点击该行时显示密码和加入按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"

    android:padding="6dip">
    <!-- Item Name -->

    <TextView
        android:id="@+id/canvasName"
        android:height="100dp"
         />
    <TextView
        android:id="@+id/canvasCreator"
        android:text="TextView" />
    <EditText
        android:id="@+id/joinCanvasPasswordTxt"
           android:visibility="invisible"
        />   
     <Button
         android:id="@+id/joinCanvasBtn"
         android:text="Join"
         android:visibility="invisible" 
         />


</LinearLayout>

请帮我找到解决方案

1 个答案:

答案 0 :(得分:0)

尝试写

int index = position - canvasListView.getFirstVisiblePosition();

而不是

int index = canvasListView.getFirstVisiblePosition() + position;