繁体   English   中英

获取ListView项的TextView文本

[英]Get the text of TextView of Item of ListView

我想获取ListView项中的TextView文本。

查看代码,您将发现我的问题。

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>

就这样!!

我想获取R.id.article_article的文本(请参见article_list_item.xml)。

我该怎么做?

这对您有用吗?

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

这个想法是,在您的SimpleAdapter ,您将使用List<Map<String,Object>> (在本例中为result )填充ListView

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

看到您的评论后进行编辑:尝试以下操作:

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

首先,忘记xml布局。 它只是数据的占位符,用于定义数据的布局,与存储数据无关。

现在,您的问题陈述可以重新描述为:单击列表项时如何从数组(或列表或数组列表或任何结果格式)中获取数据。

嗯。 现在听起来很简单。

有关技术细节,请参阅@RougeBaneling的答案。

暂无
暂无

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

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