简体   繁体   English

获取ListView项的TextView文本

[英]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. 我想获取ListView项中的TextView文本。

See the code and you will find what's my question. 查看代码,您将发现我的问题。

Activity.java: 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 : 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). 我想获取R.id.article_article的文本(请参见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 ). 这个想法是,在您的SimpleAdapter ,您将使用List<Map<String,Object>> (在本例中为result )填充ListView

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. 因此,您可以使用.get(position)List获取特定的Map<String,Object> ,然后再次使用.get("article_articleNo")从地图中获取Object

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. 首先,忘记xml布局。 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. 有关技术细节,请参阅@RougeBaneling的答案。

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

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