简体   繁体   English

从选定的Listview项获取TextView字符串资源

[英]Get TextView string resource from a selected Listview item

I didn't understand ListViews, so I read and copied the code from this website to get me rolling: https://www.sitepoint.com/starting-android-development-creating-todo-app/ 我不了解ListViews,所以我阅读并复制了该网站上的代码以使我滚动: https : //www.sitepoint.com/starting-android-development-creating-todo-app/

The website didn't provide a comprehensive breakdown of the code, but it all worked flawlessly so I've kept it as it is, bar adding 3 additional views (That are not yet used by the ListView database) and removing the delete button from item_todo.xml; 该网站没有提供完整的代码细分,但是它们都可以正常工作,因此我保持原样,禁止添加3个其他视图(ListView数据库尚未使用),并从中删除删除按钮。 item_todo.xml; I also removed the delete button's corresponding code in MainActivity. 我还删除了MainActivity中删除按钮的相应代码。

So my item_todo.xml looks like this: 所以我的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>

And my MainActivity looks like the code I linked on the website, minus the "Deleting Tasks" chapter. 我的MainActivity看起来像我在网站上链接的代码,减去“删除任务”一章。

My question is: When I click on a given list element, how do I get that ListView's position within the list, and then, how do I extract the corresponding text from the task_title TextView as a string? 我的问题是:当我单击给定的列表元素时,如何获取ListView在列表中的位置,然后如何从task_title TextView中将相应的文本提取为字符串?

If I undersand your issue correctly so you can extract a string from selected row in list view by this code. 如果我正确理解了您的问题,那么您可以通过此代码从列表视图中的选定行中提取字符串。

        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();
        }
    });

Just set OnItemClickListener on your listview. 只需在列表视图上设置OnItemClickListener。 this method will be invoked each time you click on any item in your list. 每次您单击列表中的任何项目时,都会调用此方法。 Inside OnItemClick you have the view of selected row so you can extract any child view from this layout and get your text. 在OnItemClick内部,您具有选定行的视图,因此您可以从此布局提取任何子视图并获取文本。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM