1. 程式人生 > >android ListView佈局之三(使用自定義的Adapter繫結資料,通過contextView.setTag繫結資料)有按鈕的ListView

android ListView佈局之三(使用自定義的Adapter繫結資料,通過contextView.setTag繫結資料)有按鈕的ListView

http://blog.csdn.net/chenzheng_java/article/details/6202586

最終結果圖:

程式碼結構示意圖

vlist2.xml程式碼:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayout
  3.   xmlns:android="http://schemas.android.com/apk/res/android"
  4.   android:layout_width="fill_parent"
  5.   android:layout_height
    ="fill_parent">
  6.   <ImageView
  7.     android:id="@+id/image"
  8.     android:layout_width="wrap_content"
  9.     android:layout_height="wrap_content"
  10.     android:layout_margin="5px"
  11.   />
  12.   <LinearLayout
  13.     android:layout_width="wrap_content"
  14.     android:layout_height="wrap_content"
  15.     android:orientation
    ="vertical"
  16.   >
  17.     <TextView
  18.         android:id="@+id/title"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:textColor="#FFFFFFFF"
  22.         android:textSize="22px"
  23.     />
  24.     <TextView
  25.         android:id="@+id/info"
  26.         android:layout_width
    ="wrap_content"
  27.         android:layout_height="wrap_content"
  28.         android:textColor="#FFFFFFFF"
  29.         android:textSize="13px"
  30.     />
  31.   </LinearLayout>
  32.   <Button
  33.     android:id="@+id/view_btn"
  34.     android:layout_width="wrap_content"
  35.     android:layout_height="wrap_content"
  36.     android:text="click me"
  37.     android:gravity="center"
  38.   />
  39. </LinearLayout>

listView3.java程式碼

  1. package cn.com.android2.listview;  
  2. import java.util.ArrayList;  
  3. import java.util.HashMap;  
  4. import android.app.ListActivity;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.widget.ListView;  
  9. publicclass listView3 extends ListActivity {  
  10.     @Override
  11.     publicvoid onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         // 獲取虛擬的資料,資料的格式有嚴格的要求哦
  14.         ArrayList<HashMap<String, Object>> data = getData();  
  15.         //模仿SimpleAdapter實現的自己的adapter
  16.         MyAdapter adapter = new MyAdapter(this, data);  
  17.         /** 
  18.          * 有些人很迷糊,我們都知道vlist2.xml相當於儲存一行資料的元件佈局,我們在前邊的程式碼中,都是有一個主佈局檔案main.xml的, 
  19.          * 元件佈局檔案是放在主佈局檔案上顯示的,一般程式碼中都是通過setContentView()來指定主佈局檔案的。為何這裡根本就沒有用到 
  20.          * ,但是listView還能有一個介面來呈現呢。 
  21.          * 讓我們看看setListAdapter在ListActivity中的實現, 
  22.          * public void setListAdapter(ListAdapter adapter) { 
  23.             synchronized (this) { 
  24.             ensureList(); 
  25.             mAdapter = adapter; 
  26.             mList.setAdapter(adapter); 
  27.         } 
  28.     } 
  29.     裡面呼叫了一個ensureList方法,我們再來看看這個方法: 
  30.      private void ensureList() { 
  31.         if (mList != null) { 
  32.             return; 
  33.         } 
  34.         setContentView(com.android.internal.R.layout.list_content); 
  35.     } 
  36.     現在看到了,這裡有個 setContentView方法,裡面設定了我們的元件在一個android自己提供的介面上顯示。 
  37.     原來,我們的理論還是適用的,只不過ListActivity給我進行了隱藏實現。 
  38.          */
  39.         setListAdapter(adapter);  
  40.     }  
  41.     /** 
  42.      * @author chenzheng_java 
  43.      * @description 準備一些測試資料 
  44.      * @return 一個包含了資料資訊的hashMap集合 
  45.      */
  46.     private ArrayList<HashMap<String, Object>> getData(){  
  47.         ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();  
  48.         for(int i=0;i<10;i++){  
  49.             HashMap<String, Object> tempHashMap = new HashMap<String, Object>();  
  50.             tempHashMap.put("image", R.drawable.icon);  
  51.             tempHashMap.put("title""標題"+i);  
  52.             tempHashMap.put("info""描述性資訊");  
  53.             arrayList.add(tempHashMap);  
  54.         }  
  55.         return arrayList;  
  56.     }  
  57.     @Override
  58.     protectedvoid onListItemClick(ListView l, View v, int position, long id) {  
  59.         Log.i("輸出資訊",v.toString() );  
  60.     }  
  61. 相關推薦

    android ListView佈局使用定義Adapter資料通過contextView.setTag資料按鈕ListView

    http://blog.csdn.net/chenzheng_java/article/details/6202586 最終結果圖: 程式碼結構示意圖 vlist2.xml程式碼: &

    Spring原始碼解析定義標籤的解析

    自定義標籤的解析 1、概述:通過前面的文章我們分析了預設標籤的解析,我們先回顧下自定義標籤解析所使用的方法 /** * Parse the elements at the root level in the document: * "import", "alias", "bean".

    JSP編程專題2JSP核心定義EL函數和標簽

    自定義el函數 jstl中的el函數 自定義el標簽 自定義帶屬性的el標簽 自定義el遍歷標簽 1、自定義EL函數:a、自定義類,類中的方法使用static修飾符修飾:b、該類以及函數,需要在一個擴展名為.tld(tld,tag library definition,標簽庫定義)的XML

    Android學習筆記為Dialog定義佈局並說明空指標問題

    分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

    android中的對話方塊定義對話方塊

    首先看下效果圖 下面講一下具體的實現: 1.修改系統預設的Dialog樣式(風格、主題) 2.自定義Dialog佈局檔案 3.可以自己封裝一個類,繼承自Dialog或者直接使用Dialog類來實現,為了方便以後重複使用,建議自己封裝一個Dialog類 ==

    【我的Android進階旅】定義ContentProvider

    引言 我們知道Android有四大元件,ContentProvider是其中之一,顧名思義:內容提供者。什麼是內容提供者呢?一個抽象類,可以暴露應用的資料給其他應用。應用裡的資料通常說的是資料庫,事實上普通的檔案,甚至是記憶體中的物件,也可以作為內容提供者暴露的

    【我的Android進階旅】定義控制元件使用ViewPager實現可以預覽的畫廊效果並且定義畫面切換的動畫效果的切換時間

    我們來看下效果 在這裡,我們實現的是,一個ViewPager來顯示圖片列表。這裡一個頁面,ViewPage展示了前後的預覽,我們讓預覽頁進行Y軸的壓縮,並設定透明度為0.5f,所有我們看到gif最後,左右兩邊的圖片有點朦朧感。讓預覽頁和主頁面有主從感。我們用分

    jiebasedict定義字典

    程式碼:import jieba string = "李小福是創新辦主任也是雲端計算方面的專家" # 精確模式分詞 cut_result = jieba.cut(string) print("===" * 20) print("/".join(cut_result))

    Android 環信擴充套件訊息定義訊息

    之前整合即時通訊(環信)的時候,需要用到自定義訊息的功能。而在開發過程中遇到了許多的問題,之前工作比較忙,現在有時間了記錄一下。 第一步: 首先,在聊天介面新增一個傳送擴充套件訊息的MenuItem

    Linux自動化運維Cobbler定義重裝

    localhost reboot ace koan mirrors 虛擬機 all 更換 epel Cobbler?定義重裝 當現有虛擬機運?出現故障後, 需要進?重裝操作, 可通過koan進?重裝系統 將CentOS6重裝成CentOS7 1.客戶端安裝 koan #

    Linux自動化運維Cobbler定義系統安裝

    lin gateway onf ces -a entos 網卡 定義 proc ##Cobbler自定義安裝由於 kickstart 指定某臺服務器使?某個具體的 ks ?件?較復雜,所以引? Cobbler 就變得? 常的簡單。通過物理MAC地址來區分。 Cobbler

    Linux自動化運維Cobbler定義倉庫

    之間 成對 aliyun 需要 fig kick emc pos var Cobbler?定義倉庫Cobbler會將指定的repo倉庫裏的.rpm包下載到本地,通過Cobbler安裝的系統會在本地生成對應的.repo文件,裏面的地址內容指向Cobbler的IP地址 實現該

    彈窗定義Dialog

    第一步: 給Dialog設定一個風格主題 (基本都是用這個主題)無邊框全透明背景: res/values/styles: <!--自定義dialog背景全透明無邊框theme --> <style name="MyDi

    Android 日常封裝暴力CustomViewDialog定義對話方塊

    import android.app.Dialog; import android.content.Context; import android.support.annotation.LayoutRe

    CSS頁面佈局凝膠布局

    介於流體佈局(Float Layout)和凍結佈局(Frozen Layout)間的就是凝膠布局(Jello Layout) 實現方式:在凍結佈局的基礎上,設定CSS端的margin-left/right為auto 第一步就是實現凍結佈局(請參照前一篇博文《CSS之頁面佈

    淺談對Bootstrap的看法提示框和彈出框、滾動監聽、小工具

    1.提示框和彈出框相關說明:提示框和彈出框效果類似。提示框:通過向元素新增 data-toggle="tooltip" 來建立提示框,<a href="#" data-toggle="tooltip" title="我是提示內容!">滑鼠移動到我這</a&g

    golang web實戰基於iris框架的 web小應用,數據庫采用 sqlite3

    golang mys 上傳文件 aliyun size 變量 ima appname all 一、效果:一個圖片應用 1、可上傳圖片到uploads目錄。 2、可瀏覽和評論圖片(用富文本編輯器輸入) 二、梳理一下相關知識: 1、iris框架(模板輸出,session

    《第一行程式碼Android》學習總結第定義佈局與控制元件

    1、View是Android中最基本的元件,它可以在螢幕上繪製一塊矩形區域,並在這塊區域內響應各種事件。所有控制元件都直接或間接繼承自View。 2、ViewGroup是一種特殊的View,可以包含很多子View和子ViewGroup,是一個用於放置控制元件和佈局的容器。所有佈局都直接或間

    Android ListView分兩列顯示不用定義Adapter

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pa

    Android ListView的應用如何去實現ListView控制元件定義介面卡

    稀稀拉拉學了有快1年的Android了,但是依然跟剛入門的小白一樣,用到啥學啥,上網查別人的程式碼,然後複製貼上過去,最後什麼都沒學到,現在是深有體會,我希望記錄一些知識點,踏踏實實的走好每一步,希望剛入門的小白能用到。首先Android Studio中有許多系統自帶的空間,