简体   繁体   中英

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/

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; I also removed the delete button's corresponding code in MainActivity.

So my item_todo.xml looks like this:

<?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.

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?

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. 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. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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