TShopping

 找回密碼
 註冊
搜索
查看: 1595|回復: 0

[教學] Android 獲取 Listview 內 AdapterView textview imageview資訊

[複製鏈接]
發表於 2017-2-24 02:09:27 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
演示圖片:
Screenshot.jpg

用 ListView 來秀出選單
當一堆字串顯示在 ListView 中
客製化的 View 顯示在 ListView 中
提供單選 single choice 的選單

怎麼去抓取ListView內的字串及圖像呢?


  1. listView.setAdapter(imagetextadapter);
  2.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  3.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  4.                 //Get ImageView draw form Listview view
  5.                 ImageView imageview = (ImageView) view.findViewById (R.id.imageView1);
  6.                 //Get Textview String form Listview view
  7.                 TextView pkg_label = (TextView) view.findViewById (R.id.pkg_label);
  8.                 TextView pkg_name = (TextView) view.findViewById (R.id.pkg_name);
  9.                 TextView pkg_apksize = (TextView)view.findViewById (R.id.apksize);
  10.                 TextView pkg_datasize = (TextView)view.findViewById (R.id.datasize);
  11.                 TextView pkg_cachesize = (TextView)view.findViewById (R.id.cachesize);
  12.                 TextView pkg_totalsize = (TextView)view.findViewById (R.id.totalsize);
  13.                 String tmp1 = pkg_name.getText().toString();
  14.                 String tmp2 = pkg_label.getText().toString();
  15. }
  16. }
複製代碼
view.findViewById 來擷取單選 的選單
假設要抓取pkg_label內的String
宣告
  1. TextView pkg_label = (TextView) view.findViewById (R.id.pkg_label);
複製代碼

抓取字串用
  1. String tmp1 = pkg_name.getText().toString();
複製代碼
這樣就可以了

圖像同理

以下是XML布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/LinearLayout2"
  4.     android:layout_width="wrap_content"
  5.     android:layout_height="100dp"
  6.     android:orientation="horizontal" >
  7.     <ImageView
  8.         android:id="@+id/imageView1"
  9.         android:layout_width="70dp"
  10.         android:layout_height="70dp"
  11.         android:src="@drawable/ic_launcher" />
  12.     <LinearLayout
  13.         android:id="@+id/LinearLayoutlist"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="70dp"
  16.         android:orientation="vertical" >

  17.         <TextView
  18.             android:id="@+id/pkg_name"
  19.             android:textSize="16dp"
  20.             android:textStyle="bold"
  21.             android:textColor="@android:color/holo_orange_light"
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content" />

  24.         <TextView
  25.             android:id="@+id/pkg_label"
  26.             android:textSize="12dp"
  27.             android:layout_width="wrap_content"
  28.             android:layout_height="wrap_content" />
  29.         <LinearLayout
  30.             android:id="@+id/LinearLayoutSize"
  31.             android:layout_width="wrap_content"
  32.             android:layout_height="13dp"
  33.             android:orientation="horizontal" >
  34.             <TextView
  35.                 android:id="@+id/apk"
  36.                 android:textSize="11dp"
  37.                 android:layout_width="wrap_content"
  38.                 android:layout_height="wrap_content"
  39.                 android:text="Apk:" />
  40.             <TextView
  41.                 android:id="@+id/apksize"
  42.                 android:textSize="11dp"
  43.                 android:layout_width="wrap_content"
  44.                 android:layout_height="wrap_content" />
  45.             <TextView
  46.                 android:id="@+id/data"
  47.                 android:textSize="11dp"
  48.                 android:layout_marginLeft="20px"
  49.                 android:layout_width="wrap_content"
  50.                 android:layout_height="wrap_content"
  51.                 android:text="Data:" />
  52.             <TextView
  53.                 android:id="@+id/datasize"
  54.                 android:textSize="11dp"
  55.                 android:layout_width="wrap_content"
  56.                 android:layout_height="wrap_content" />
  57.             <TextView
  58.                 android:id="@+id/cache"
  59.                 android:textSize="11dp"
  60.                 android:layout_marginLeft="20px"
  61.                 android:layout_width="wrap_content"
  62.                 android:layout_height="wrap_content"
  63.                 android:text="Cache:" />
  64.             <TextView
  65.                 android:id="@+id/cachesize"
  66.                 android:textSize="11dp"
  67.                 android:layout_width="wrap_content"
  68.                 android:layout_height="wrap_content" />
  69.             <TextView
  70.                 android:id="@+id/total"
  71.                 android:textSize="11dp"
  72.                 android:layout_marginLeft="20px"
  73.                 android:layout_width="wrap_content"
  74.                 android:layout_height="wrap_content"
  75.                 android:text="Total:" />
  76.             <TextView
  77.                 android:id="@+id/totalsize"
  78.                 android:textSize="11dp"
  79.                 android:layout_width="wrap_content"
  80.                 android:layout_height="wrap_content" />
  81.         </LinearLayout>
  82.         <TextView
  83.             android:id="@+id/linkfrom"
  84.             android:textSize="13dp"
  85.             android:layout_width="wrap_content"
  86.             android:layout_height="wrap_content"
  87.             android:text="Link form:"
  88.             android:layout_alignParentRight="true" />
  89.     </LinearLayout>
  90.     <RelativeLayout
  91.         android:layout_width="fill_parent"
  92.         android:layout_height="50dp" >
  93.     </RelativeLayout>
  94. </LinearLayout>
複製代碼


參考文章http://julianchu.net/2015/09/26-implement-listview.html



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-19 18:19 , Processed in 0.065071 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表