返回列表 發帖

Android App - Simple Adapter

ListView 採用Simple Adapter, 一行顯示一項資料。

在主程式 testActivity.java 內有以下相關程式碼
  1. public class testActivity extends Activity {
  2. ArrayList<HashMap<String, Object>> listItem6 = new ArrayList<HashMap<String, Object>>();


  3.   public void onCreate(Bundle savedInstanceState) {  
  4.   super.onCreate(savedInstanceState);
  5.   setContentView(R.layout.main);
  6.   ListView lv3 = (ListView)findViewById(R.id.ListView1);        
  7.   listItem5 = new ArrayList<String>();                                          
  8.   final SimpleAdapter listItemAdapter3 = new SimpleAdapter      
  9.               (this,listItem6,R.layout.list_items,
  10.               new String[] {"ItemTitle"},            
  11.               new int[] {R.id.ItemTitle} );

  12.   lv3.setAdapter(listItemAdapter3);         
  13.   lv3.setOnItemClickListener(new OnItemClickListener() {
  14.   public void onItemClick(AdapterView<?> a, View v, int position, long id) {
  15.       String s1= listItem6.get(position).values().toString();
  16.       String s2= copystring.substring(1,s1.length()-1);
  17.       //remove two char:  [ and ]                  
  18.      }
  19.     });              
  20.         
  21.       listItem6.clear();
  22.       // 加一行資料                     
  23.       HashMap<String, Object> map = new HashMap<String, Object>();
  24.       map.put("ItemTitle",s2);
  25.       listItem6.add(map);            
  26.       listItemAdapter3.notifyDataSetChanged();
  27.             
  28.       // 改一行資料                     
  29.       HashMap<String, Object> ro = new HashMap<String, Object>();
  30.       ro.put("ItemTitle",s2);
  31.       listItem6.set(position, ro);     
  32.       listItemAdapter3.notifyDataSetChanged();
  33.   };}
複製代碼
在 main.xlm 內有 ListView
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical"
  6.     android:focusableInTouchMode="true">

  7.     <ListView
  8.         android:id="@+id/ListView1"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="59dp"
  11.         android:layout_weight="2.0"
  12.         android:transcriptMode="alwaysScroll" >

  13.     </ListView>
  14. </LinearLayout>
複製代碼
建立 list_items.xml
  1. <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout      
  2.     android:id="@+id/RelativeLayout01"      
  3.     android:layout_width="fill_parent"      
  4.     xmlns:android="http://schemas.android.com/apk/res/android"      
  5.     android:layout_height="wrap_content"      
  6.     android:paddingBottom="4dip"      
  7.     android:paddingLeft="12dip"      
  8.     android:paddingRight="12dip">   

  9. <TextView   
  10.     android:id="@+id/ItemTitle"   
  11.     android:layout_width="fill_parent"   
  12.     android:layout_height="wrap_content"   
  13.     android:height="26dp"   
  14.     android:text="TextView01"   
  15.     android:textSize="20dip"
  16.     android:textColor="#FFFFFF"/>  
  17. </RelativeLayout>
複製代碼
Bill Tang     MSN:billtang@openplatform.com.hk
Openplatform Technology Co.,Ltd. 資訊坊科技有限公司  
無線工程施工、方案設計、無線產品、天饋材料、終端設備綜合供應商
Tel: 852-27491011  Fax: 852-81483532

返回列表