gpt4 book ai didi

java - 从选定的列表项中选择一个 TextView

转载 作者:行者123 更新时间:2023-11-30 02:40:10 25 4
gpt4 key购买 nike

这是我的代码:

    mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

// TODO Auto-generated method stub
View vie = (View)parent.getParent();
TextView tv = (TextView) vie.findViewById(R.id.tv_id);
String genus = (String) tv.getText();
Toast.makeText(getBaseContext(), genus+"--"+id, Toast.LENGTH_SHORT).show();
}
});

return adapter;
}

每次它为每个项目返回相同的值。这是我的列表项:

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

<TextView
android:id="@+id/tv_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:textStyle="bold" />

<ImageView
android:id="@+id/iv_flag"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_below="@id/tv_country"
android:layout_centerVertical="true"
android:padding="5dp"
/>

<TextView
android:id="@+id/tv_country_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_flag"
android:layout_below="@id/tv_country" />
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_country_details"
/>
</RelativeLayout>

这是我的 View 文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
android:id="@+id/lv_countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".NearbyGymList" />
<Button android:id="@+id/btnNoResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lv_countries"
android:text="No More Results"/>

</RelativeLayout>

如何解决这个问题?

最佳答案

删除这一行:

View vie = (View)parent.getParent();

并更改为使用来自参数的 View ,因为它是被单击的项目的 View :

    mListView.setOnItemClickListener(new OnItemClickListener() {

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

// TODO Auto-generated method stub
//Remove this line: View vie = (View)parent.getParent();
//Use view because it is the view of item which is clicked.
TextView tv1 = (TextView) view.findViewById(R.id.tv_id);

//Add this
TextView tv2 = (TextView) view.findViewById(R.id.tv_country_details);
String genus = (String) tv1.getText().toString();
String genus2 = (String) tv2.getText().toString();

Toast.makeText(getBaseContext(),genus+"--"+id+" "+genus2 , Toast.LENGTH_SHORT).show();
}
});

return adapter;
}

关于java - 从选定的列表项中选择一个 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25887736/

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