【发布时间】: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 中提取相应的文本作为字符串?
【问题讨论】:
-
显示您尝试过的内容?