[英]How to get a textView value from ListView adapter onClick button inside same view?
我有一个列表视图,其中包含用作按钮的imageView和一个TextView。
我想要的是当我单击按钮(例如,列表视图中的第三项)时,我想在单击与文本视图相同位置的按钮时获得textview中的文本。
我会尝试
final TextView txt= (TextView) findViewById(R.id.txt);
String txt2 = txt.getText().toString();
越来越少的工作。
我的问题太奇怪了,我会尝试用图片解释
当我单击按钮时,无论位置如何,总会返回显示在屏幕上第一位置的textview(不在屏幕上的listview上)
例如
在这种情况下,(我的屏幕显示为红色正方形)如果我从项目1,2或3单击按钮,它将返回tv_1
。
其他示例(再次在其屏幕上显示红色方块)
现在,如果我单击项目2,3或4中的按钮,它将返回tv_2
也就是说,它始终在屏幕上显示可见项目列表的顶部
我的密码
public void btn(View view)
{
final TextView tv_id = (TextView) findViewById(R.id.tv_id);
String txt = tv_id.getText().toString();
Log.i(LOGTAG, "" + txt);
}
有什么建议么?
更新
问题是因为我需要使用final
变量来仅获取该textview,然后其报告的第一项显示在屏幕上,但是...为什么呢? 或如何解决?
更新2我的XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_marginLeft="26dp"
android:layout_marginTop="26dp"
android:layout_marginRight="26dp"
android:id="@+id/lay_fondo">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/cardview_notify"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="90dp"
android:layout_height="match_parent"
android:background="@color/primary_green"
android:id="@+id/lay_prioridad"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="580dp"
android:layout_height="match_parent"
android:layout_weight="0.54">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="id"
android:id="@+id/tv_id"
android:layout_gravity="center_vertical"
android:textColor="@color/black"
android:textSize="30sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="26dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/img_favorito"
android:src="@drawable/star"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:layout_onClick="btn"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
尝试替换下面的行
final TextView tv_id = (TextView) findViewById(R.id.tv_id);
与
TextView tv_id =(TextView)((视图)view.getParent())。findViewById(R.id.tv_id);
上一行在给定视图中找到textview,
要么
添加一个OnItemClickListener()
并列出视图
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
String txt = tv_id.getText().toString();
Log.i(LOGTAG, "" + txt);
}
});
尝试将onItemClickListener设置为listView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final TextView txt= (TextView) view.findViewById(R.id.txt);
String txt2 = txt.getText().toString();
Log.i(LOGTAG, "" + txt2);
}
});
要么
在您的内部设置TextView的点击监听器
getView(int position, View convertView, ViewGroup parent)
我的建议;
tv_id仅引用一项(R.if.tv_id)。 为此,无论您将按钮放在哪里,它都将始终调用tv_id处理的代码。
我建议您引用列表视图,并在listView上设置setonclicklistener。
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
为什么要以如此复杂的方式获取文本? 我假设您拥有自定义的ListView
适配器(例如MyAdapter
)和带有吸气剂的项目(例如MyItem
)(例如MyItem :: getText()
,不要介意C ++样式)。
那么您的方法可能是:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Probably this check is not that necessary but better safe than sorry
if (listView.getAdapter() instanceof MyAdapter) {
MyAdapter adapter = (MyAdapter)listView.getAdapter();
String text = adapter.getItem(position).getText();
// Do whatever you need with the text
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.