gpt4 book ai didi

从 OnItemClickMethod 中提取 android View

转载 作者:行者123 更新时间:2023-11-29 14:55:22 25 4
gpt4 key购买 nike

我有一个 ListView ,在这个 ListView 的适配器中我有一个方法

public void onItemClick(AdapterView<?> AdapterView, View View, 
int position, long id)

每行列表包含3个 View

行的xml是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@android:color/white" android:padding="5dp">

<TextView android:id="@+id/name" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp" android:textStyle="bold" android:textColor="@android:color/black"
android:layout_marginTop="10dp" android:focusable="false">
</TextView>

<TextView android:id="@+id/street"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/name"
android:textColor="@android:color/black" android:focusable="false">
</TextView>

<ImageView android:id="@+id/pic" android:src="@drawable/pic"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_marginRight="10dp"
android:layout_below="@id/name" android:focusable="false">
</ImageView>

</RelativeLayout>

在 OnItemClickMethod 中,我想在这 3 个 View 上找到点击事件。我怎样才能做到这一点?

注意:如果我使用 view.getId(),它会为第一行返回 -1。所以我无法使用这个。

if(view.getId() == R.id.name){
dotask();
}

更新:我只是在 getView 方法中调用了 onClick 方法

setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context,position.toString(), Toast.LENGTH_LONG).show();
}
});

我的问题就解决了。

感谢 FunkTheMonk 和 Vikky。

最佳答案

根据你的xml文件,这里的onItemClick View view指的是RelativeLayout而不是textviews。

public void onItemClick(AdapterView<?> AdapterView, View View, int position, long id) {

TextView name = View.findViewById ( R.id.name );
for other views .......
}

关于从 OnItemClickMethod 中提取 android View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8164424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com