简体   繁体   中英

Get the text of TextView of Item of ListView

I want to get the text of a TextView that is in a ListView's item.

See the code and you will find what's my question.

Activity.java:

SimpleAdapter adapter = new SimpleAdapter(
                rootView.getContext(),
                result,
                R.layout.article_list_item,
                new String[]{"article_title", "article_PubDate", "article_category", "article_articleNo"},
                new int[]{R.id.article_title, R.id.article_PubDate, R.id.article_category, R.id.article_articleNo});
ListView articleItem.setAdapter(adapter);
articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //I want get the text of R.id.article_articleNo(see article_list_item.xml)?
                //What should I do?
            }
});

article_list_item.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/article">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="文章标题"
    android:id="@+id/article_title"
    android:layout_gravity="center_horizontal" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="30-Apr-2014"
        android:id="@+id/article_PubDate"
        android:layout_gravity="center_horizontal"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="personal_finance"
        android:id="@+id/article_category"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9312"
        android:id="@+id/article_articleNo"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

</LinearLayout>

That's all!!

I want to get the text of R.id.article_article (see article_list_item.xml).

How can I do it?

Does this work for you?

articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String text = result.get(position).get("article_articleNo");
            //^^ does not necessarily need to be a string,
            //   make this whatever data type you are storing in the map
        }
});

The idea is that in your SimpleAdapter , you are populating your ListView with a List<Map<String,Object>> (in this case named result ).

Therefore, you can get that specific Map<String,Object> from the List by using .get(position) , then once again using .get("article_articleNo") to get the Object from the map.

Edit after seeing your comment: Try this:

articleItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView tv = (TextView) view.findViewById(R.id.article_articleNo);
        String text = tv.getText().toString();
    }
});

First of all, forget about the xml layout. It's just a placeholder for data, which is used to define the layout of the data, and has got nothing to do with storing the data.

Now, your problem statement can be re-stated as: how to fetch data from an array (or list or arraylist or whatever the format of your result is) on clicking list item.

Hmm. Sounds simple now.

Refer to @RougeBaneling's answer for technical details.

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