在 “顯示簡單的清單內容” 中提到 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 的元素,如下

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

接著把 map 加到 list 中

list.add(map);

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

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

 

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

  <?xml version="1.0" encoding="utf-8"?>
  <!-- Copyright (C) 2006 The Android Open Source Project
   
       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at
            http://www.apache.org/licenses/LICENSE-2.0
       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
  -->
  <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minHeight="?android:attr/listPreferredItemHeightSmall"
      android:mode="twoLine" >
   
      <TextView
          android:id="@+id/date"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="2012/01/01"
          android:textAppearance="?android:attr/textAppearanceSmall" />
   
      <TextView
          android:id="@+id/money"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/date"
          android:layout_toRightOf="@+id/date"
          android:text="-$99999"
          android:maxEms="4"
          android:singleLine="true"
          android:textAppearance="?android:attr/textAppearanceListItem" />
   
      <TextView
          android:id="@+id/detail"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/date"
          android:layout_toRightOf="@+id/date"
          android:layout_marginLeft="120dp"
          android:text="這是備註"
          android:maxLines="2"
          android:ellipsize="end"
          android:textAppearance="?android:attr/textAppearanceSmall" />
   
  </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 傳入即可,如下

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

 

最後的程式碼如下

   
  public class Table_widget4 extends Activity{
      String[] strArr1 = {"2012/01/01","2012/01/02","2012/01/03","2012/01/04","2012/01/05"
              ,"2012/01/01","2012/01/02","2012/01/03","2012/01/04","2012/01/05"
              ,"2012/01/01","2012/01/02","2012/01/03","2012/01/04","2012/01/05"};
      String[] strArr2 = {"111111","222222","3333333","4444444","55555555555555555"
              ,"111111","222222","3333333","4444444","55555555555555555"
              ,"111111","222222","3333333","4444444","55555555555555555"};
      String[] strArr3 = {"這是備註1","這是備註222222222222222222222222","這是備註3","這是XXXOOO","這是備註"
              ,"這是備註1","這是備註222222","這是備註3","這是XXXOOO","這是備註"
              ,"這是備註1","這是備註222222","這是備註3","這是XXXOOO","這是備註"};
      ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
      ListView lv;
      public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.tabwidget4);
          initXml();
      }
      @SuppressLint("NewApi")
      void initXml(){
   
          for(int i=0; i<strArr1.length; i++){
              HashMap<String, String> map = new HashMap<String, String>();
              map.put("str1", strArr1[i]);
              map.put("str2", strArr2[i]);
              map.put("str3", strArr3[i]);
              list.add(map);
          }
          SimpleAdapter sa = new SimpleAdapter(
                  this,
                  list,
                  R.layout.test2,
                  new String[]{"str1","str2","str3"},
                  new int[] { R.id.date, R.id.money , R.id.detail});
          lv = (ListView) findViewById(R.id.listview1);
          lv.setAdapter(sa);
      }
  }

 

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