簡體   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