Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取对ListView每行中的视图的访问权限_Android_Android Listview_Togglebutton_Onclicklistener - Fatal编程技术网

Android 获取对ListView每行中的视图的访问权限

Android 获取对ListView每行中的视图的访问权限,android,android-listview,togglebutton,onclicklistener,Android,Android Listview,Togglebutton,Onclicklistener,我有一个列表视图,对于列表视图的每一行,我有一个图像视图,2个文本视图视图和1个切换按钮 如何访问行内的视图(例如切换按钮) 我试过使用一个OnClickListener,但它不起作用,我也不知道为什么。以下是我尝试过的: @Override public void onListItemClick(ListView parent, View view, int position, long id){ ToggleButton tb2 = (ToggleButton)parent.fi

我有一个
列表视图
,对于
列表视图
的每一行,我有一个
图像视图
,2个
文本视图
视图和1个
切换按钮

如何访问行内的视图(例如
切换按钮

我试过使用一个
OnClickListener
,但它不起作用,我也不知道为什么。以下是我尝试过的:

 @Override
 public void onListItemClick(ListView parent, View view, int position, long id){
    ToggleButton tb2 = (ToggleButton)parent.findViewById(R.id.star);
    if(tb2.isChecked()){
    Toast.makeText(MainActivity.this, "TOOGLE BUTTON TEST", Toast.LENGTH_LONG).show();
    }
}
我的行布局:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/list_selector_background"
    android:clickable="true"
    android:gravity="fill_horizontal"
    android:longClickable="true"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingBottom="8dip"
    android:paddingTop="5dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/contact_user" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:gravity="fill"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/nameClient"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ffffff" />
        <TextView
            android:id="@+id/accountClient"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#483ca0" />
    </LinearLayout>

    <ToggleButton
        android:id="@+id/star"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/list_star_selector"
        android:clickable="true"
        android:enabled="true"
        android:inputType="none"
        android:textOff=""
        android:textOn="" />

</TableRow>

试试看


ListView父视图
是父视图,
view视图
是您实际单击的视图,包含您需要的子视图

在您的示例中,
parent
是您的列表视图,
view
是在ListView中单击的视图。您可以使用
view
或(来自android文档):子类可以调用getListView().getItemAtPosition(position),如果它们需要访问与所选项目关联的数据。

一种方法是您可以在自定义适配器类中定义侦听器

第二种方法是

ToggleButton=(ToggleButton)view.findViewBtId(R.id.star)


试试看;-)

我建议在适配器的getView()中将onClickListener实际设置为视图的背景。通过这样做,您可以访问convertView,该视图在列表视图滚动时循环使用,并可以根据您的状态对其进行修改


另外,请确保在设置视图的标记后再进行设置,这样它就不会循环使用您的侦听器并给您带来奇怪的bug

你好,米哈伊尔。谢谢你的回复。但不要这样工作。我用我的xml为listView的每一行更新我的问题。我不知道是什么问题……如果添加断点,调试器是否会在onClickListener中停止?在findViewById之后的tb2中是否有任何值?
ToggleButton tb2 = (ToggleButton)view.findViewById(R.id.star);
public void onListItemClick(ListView parent, View view, int position, long id){
    ToggleButton tb2 = (ToggleButton)view.findViewById(R.id.star);
    if(tb2.isChecked()){
        Toast.makeText(MainActivity.this, "I clicked the ToggleButton on row " + position, Toast.LENGTH_LONG).show();
    }
}