顯示自訂的清單內容(ArrayList+SimpleAdapter+ListView)


在 "顯示簡單的清單內容" 中提到 String + ArrayAdapter + ListView 可達到簡單的清單顯示(單行單筆資訊),如下圖每行只有顯示1個字串(aaaaaa,bbbbbbb等等)



















那如果想達到單行多筆資訊顯示就必須使用 ArrayList + SimpleAdapter + ListView 才能達到需求,如下圖每行有 3 個資訊(2012/01/01 , 111111 , 這是備註1 )


















首先是 ArrayList ,它的作用如同上述範例中的 String 一樣,負責提供顯示的資訊(3個字串),在這裡還必須搭配 map 來使用, map 提供 key - value 的對應型態, 1個 key 對應 1個 value ,由於我們要加入 3 個資訊, map 也必須放入 3 個 key - value 的元素,如下

   1:  map.put("str1", "2012/01/01");
   2:  map.put("str2", "111111");
   3:  map.put("str3", "這是備註1");

接著把 map 加到 list 中

   1:  list.add(map);

這樣就完成 3 個資訊的顯示,接著為 SimpleAdapter 的使用,和 ArrayAdapter 一樣提供顯示資訊(ArrayList)與清單(ListView)的橋樑,如下

   1:          SimpleAdapter sa = new SimpleAdapter(
   2:                  this, 
   3:                  list, 
   4:                  R.layout.test2,
   5:                  new String[]{"str1","str2","str3"},
   6:                  new int[] { R.id.date, R.id.money , R.id.detail});

建立需要 5 個參數,第 1個參數為 Context ,傳入自己(this)即可,第 2 個參數為資訊內容,就是剛剛提到的 ArrayList , 第 3 個參數為顯示資訊的格式,必須在 xml 中定義, R.layout.test2 如下

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <!-- Copyright (C) 2006 The Android Open Source Project
   3:   
   4:       Licensed under the Apache License, Version 2.0 (the "License");
   5:       you may not use this file except in compliance with the License.
   6:       You may obtain a copy of the License at
   7:    
   8:            http://www.apache.org/licenses/LICENSE-2.0
   9:    
  10:       Unless required by applicable law or agreed to in writing, software
  11:       distributed under the License is distributed on an "AS IS" BASIS,
  12:       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13:       See the License for the specific language governing permissions and
  14:       limitations under the License.
  15:  -->
  16:  <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
  17:      android:layout_width="match_parent"
  18:      android:layout_height="wrap_content"
  19:      android:minHeight="?android:attr/listPreferredItemHeightSmall"
  20:      android:mode="twoLine" >
  21:   
  22:      <TextView
  23:          android:id="@+id/date"
  24:          android:layout_width="wrap_content"
  25:          android:layout_height="wrap_content"
  26:          android:text="2012/01/01"
  27:          android:textAppearance="?android:attr/textAppearanceSmall" />
  28:   
  29:      <TextView
  30:          android:id="@+id/money"
  31:          android:layout_width="wrap_content"
  32:          android:layout_height="wrap_content"
  33:          android:layout_below="@+id/date"
  34:          android:layout_toRightOf="@+id/date"
  35:          android:text="-$99999"
  36:          android:maxEms="4"
  37:          android:singleLine="true"
  38:          android:textAppearance="?android:attr/textAppearanceListItem" />
  39:   
  40:      <TextView
  41:          android:id="@+id/detail"
  42:          android:layout_width="wrap_content"
  43:          android:layout_height="wrap_content"
  44:       
  45:          android:layout_below="@+id/date"
  46:          android:layout_toRightOf="@+id/date"
  47:          android:layout_marginLeft="120dp"
  48:          android:text="這是備註"
  49:          
  50:          android:maxLines="2"
  51:          android:ellipsize="end"
  52:          android:textAppearance="?android:attr/textAppearanceSmall" />
  53:   
  54:  </TwoLineListItem>

內容其實很簡單,就是 3 個 TextView 的定義也代表資訊的顯示格式,第 4 和 5 個參數代表對應的值,如 "str1" 就是對應 R.id.date(也就是顯示資訊的 2012/01/01 ), "str2" 對應 R.id.money(為顯示資訊的 111111 ) , "str3" 對應 R.id.detail(為顯示資訊的 這是備註1),如此完成 SimpleAdapter 的建立,最後是 ListView 的建立,只需要設定佈局中的格式,以及將剛剛建立的 SimpleAdapter 傳入即可,如下

   1:  lv = (ListView) findViewById(R.id.listview1);
   2:  lv.setAdapter(sa);

最後的程式碼如下

   1:   
   2:  public class Table_widget4 extends Activity{
   3:      
   4:      String[] strArr1 = {"2012/01/01","2012/01/02","2012/01/03","2012/01/04","2012/01/05"
   5:              ,"2012/01/01","2012/01/02","2012/01/03","2012/01/04","2012/01/05"
   6:              ,"2012/01/01","2012/01/02","2012/01/03","2012/01/04","2012/01/05"};
   7:      
   8:      String[] strArr2 = {"111111","222222","3333333","4444444","55555555555555555"
   9:              ,"111111","222222","3333333","4444444","55555555555555555"
  10:              ,"111111","222222","3333333","4444444","55555555555555555"};
  11:      
  12:      String[] strArr3 = {"這是備註1","這是備註222222222222222222222222","這是備註3","這是XXXOOO","這是備註"
  13:              ,"這是備註1","這是備註222222","這是備註3","這是XXXOOO","這是備註"
  14:              ,"這是備註1","這是備註222222","這是備註3","這是XXXOOO","這是備註"};
  15:      
  16:      ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
  17:      
  18:      ListView lv;
  19:      
  20:      public void onCreate(Bundle savedInstanceState){
  21:          super.onCreate(savedInstanceState);
  22:          
  23:          setContentView(R.layout.tabwidget4);
  24:          
  25:          initXml();
  26:          
  27:      }
  28:      
  29:      @SuppressLint("NewApi")
  30:      void initXml(){
  31:   
  32:          for(int i=0; i<strArr1.length; i++){
  33:              
  34:              HashMap<String, String> map = new HashMap<String, String>();
  35:              
  36:              map.put("str1", strArr1[i]);
  37:              map.put("str2", strArr2[i]);
  38:              map.put("str3", strArr3[i]);
  39:              
  40:              list.add(map);            
  41:          }
  42:          
  43:          
  44:          
  45:          SimpleAdapter sa = new SimpleAdapter(
  46:                  this, 
  47:                  list, 
  48:                  R.layout.test2,
  49:                  new String[]{"str1","str2","str3"},
  50:                  new int[] { R.id.date, R.id.money , R.id.detail});
  51:          
  52:          
  53:          
  54:          lv = (ListView) findViewById(R.id.listview1);
  55:          lv.setAdapter(sa);
  56:          
  57:          
  58:          
  59:      }
  60:      
  61:      
  62:  }


其中關於資訊的顯示有一些小技巧,如指定顯示單行,限制字數等等,都在 R.layout.test2 中













0 意見:

張貼留言

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews