TShopping
標題:
Android 獲取 Listview 內 AdapterView textview imageview資訊
[打印本頁]
作者:
woff
時間:
2017-2-24 02:09
標題:
Android 獲取 Listview 內 AdapterView textview imageview資訊
演示圖片:
Screenshot.jpg
(141.41 KB, 下載次數: 2)
下載附件
保存到相冊
2017-2-24 02:00 上傳
用 ListView 來秀出選單
當一堆字串顯示在 ListView 中
客製化的 View 顯示在 ListView 中
提供單選 single choice 的選單
怎麼去抓取ListView內的字串及圖像呢?
listView.setAdapter(imagetextadapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Get ImageView draw form Listview view
ImageView imageview = (ImageView) view.findViewById (R.id.imageView1);
//Get Textview String form Listview view
TextView pkg_label = (TextView) view.findViewById (R.id.pkg_label);
TextView pkg_name = (TextView) view.findViewById (R.id.pkg_name);
TextView pkg_apksize = (TextView)view.findViewById (R.id.apksize);
TextView pkg_datasize = (TextView)view.findViewById (R.id.datasize);
TextView pkg_cachesize = (TextView)view.findViewById (R.id.cachesize);
TextView pkg_totalsize = (TextView)view.findViewById (R.id.totalsize);
String tmp1 = pkg_name.getText().toString();
String tmp2 = pkg_label.getText().toString();
}
}
複製代碼
用
view.findViewById 來擷取
單選 的選單
假設要抓取
pkg_label內的String
宣告
TextView pkg_label = (TextView) view.findViewById (R.id.pkg_label);
複製代碼
抓取字串用
String tmp1 = pkg_name.getText().toString();
複製代碼
這樣就可以了
圖像同理
以下是XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:id="@+id/LinearLayoutlist"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:orientation="vertical" >
<TextView
android:id="@+id/pkg_name"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@android:color/holo_orange_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/pkg_label"
android:textSize="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/LinearLayoutSize"
android:layout_width="wrap_content"
android:layout_height="13dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/apk"
android:textSize="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apk:" />
<TextView
android:id="@+id/apksize"
android:textSize="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/data"
android:textSize="11dp"
android:layout_marginLeft="20px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data:" />
<TextView
android:id="@+id/datasize"
android:textSize="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/cache"
android:textSize="11dp"
android:layout_marginLeft="20px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cache:" />
<TextView
android:id="@+id/cachesize"
android:textSize="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/total"
android:textSize="11dp"
android:layout_marginLeft="20px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total:" />
<TextView
android:id="@+id/totalsize"
android:textSize="11dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/linkfrom"
android:textSize="13dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link form:"
android:layout_alignParentRight="true" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp" >
</RelativeLayout>
</LinearLayout>
複製代碼
參考文章
http://julianchu.net/2015/09/26-implement-listview.html
歡迎光臨 TShopping (http://www.tshopping.com.tw/)
Powered by Discuz! X3.2