【问题标题】:Get TextView string resource from a selected Listview item从选定的 Listview 项中获取 TextView 字符串资源
【发布时间】:2016-12-27 17:46:25
【问题描述】:

我不懂 ListViews,所以我从这个网站上阅读并复制了代码以使我滚动:https://www.sitepoint.com/starting-android-development-creating-todo-app/

该网站没有提供代码的全面细分,但它都运行完美,所以我保持原样,添加 3 个额外的视图(ListView 数据库尚未使用)并删除从 item_todo.xml 中删除按钮;我还删除了 MainActivity 中删除按钮的相应代码。

所以我的 item_todo.xml 看起来像这样:

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


    <TextView
        android:id="@+id/task_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="6dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="10dp"
        android:text="Mark Skidder"
        android:typeface="serif"
        android:textSize="40sp" />


    <TextView
        android:id="@+id/characterWeightDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/characterNameDisplay"
        android:layout_below="@+id/characterNameDisplay"
        android:text="120/213"
        android:typeface="serif"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/characterLevelDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/imageView2"
        android:layout_below="@+id/characterNameDisplay"
        android:text="Level 14"
        android:typeface="serif"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/characterWeightDisplay"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="25dp"
        android:background="@color/colorGrey" />


</RelativeLayout>

我的 MainActivity 看起来就像我在网站上链接的代码,减去了“删除任务”一章。

我的问题是: 当我单击给定的列表元素时,如何获取该 ListView 在列表中的位置,然后,如何从 task_title TextView 中提取相应的文本作为字符串?

【问题讨论】:

  • 显示您尝试过的内容?

标签: android database listview


【解决方案1】:

如果我正确理解了您的问题,那么您可以通过此代码从列表视图中的选定行中提取字符串。

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            TextView textView = (TextView)view.findViewById(R.id.task_title);
            String text = textView.getText().toString();
        }
    });

只需在您的列表视图上设置 OnItemClickListener。每次单击列表中的任何项目时都会调用此方法。 在 OnItemClick 内部,您可以查看所选行的视图,因此您可以从此布局中提取任何子视图并获取文本。 希望对您有所帮助。

【讨论】:

  • 这应该可以满足您的需求。此外,onItemClick() 也有一个 'position' 参数,它应该给你在列表中的位置。
  • 完美!非常感谢,你的回答很清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 2014-05-11
  • 2018-03-23
相关资源
最近更新 更多