ID #59438

动态添加lsitView

这个很简单 就是为了说明别忘了adapter.notifyDataSetChanged();

唤醒一下,其实这个唤醒也就是在buutton下用 或者在别的控件下调用 如果是在getView下就不必了

Java代码 
  1. ublic class DynamicListItems extends ListActivity {  
  2.     private static final String         ITEM_KEY    = "key";  
  3.     ArrayList<HashMap<String, String>>  list        = new ArrayList<HashMap<String, String>>();  
  4.     private SimpleAdapter               adapter;  
  5.     private EditText                    newValue;  
  6.   
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.dynamic_list);  
  11.         newValue = (EditText) findViewById(R.id.new_value_field);  
  12.   
  13.         setListAdapter(new SimpleAdapter(this, list, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }));  
  14.         ((ImageButton) findViewById(R.id.button)).setOnClickListener(getBtnClickListener());  
  15.     }  
  16.   
  17.     private OnClickListener getBtnClickListener() {  
  18.         return new OnClickListener() {  
  19.             public void onClick(View view) {  
  20.                 try {  
  21.                     HashMap<String, String> item = new HashMap<String, String>();  
  22.                     item.put(ITEM_KEY, newValue.getText().toString());  
  23.                     list.add(item);  
  24.                     adapter.notifyDataSetChanged();  
  25.                 } catch  (NullPointerException e) {  
  26.                     Log.i("[Dynamic Items]""Tried to add null value");  
  27.                 }  
  28.             }  
  29.         };  
  30.     }  
  31. }  

 

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.        
  8.       
  9.      <ListView android:id="@+id/android:list"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="0px"  
  12.         android:layout_weight="1"  
  13.         android:cacheColorHint="#00000000">  
  14.      </ListView>  
  15.     
  16.     
  17.     <LinearLayout  
  18.         android:orientation="horizontal"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginTop="6px"  
  22.         android:layout_marginBottom="8px"  
  23.         android:layout_marginLeft="8px"  
  24.         android:layout_marginRight="8px"  
  25.         android:background="#00000000">  
  26.           
  27.           
  28.         <ImageButton  
  29.         android:id="@+id/button"   
  30.             android:layout_width="100px"  
  31.             android:layout_height= "100px"  
  32.             android:src="@android:drawable/ic_menu_add"  />    
  33.     </LinearLayout>         
  34.        
  35.     <LinearLayout  
  36.         android:orientation="vertical"  
  37.         android:layout_width="230px"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_marginTop="8px"  
  40.         android:layout_marginBottom="8px"  
  41.         android:layout_marginLeft="0px"  
  42.         android:layout_marginRight="28px"  
  43.         android:background="#cccccc00" >  
  44.       
  45.         <EditText android:id="@+id/new_value_field"  
  46.             android:textColor="#020905"  
  47.             android:textSize="18sp"  
  48.             android:layout_marginTop="6px"  
  49.             android:layout_marginBottom="8px"  
  50.             android:layout_marginLeft="12px"  
  51.             android:layout_marginRight="12px"  
  52.             android:layout_width="210px"  
  53.             android:layout_height="wrap_content"  
  54.            
  55.             android:layout_weight="1"/>  
  56.       
  57.     </LinearLayout>  
  58.        
  59.     
  60. </LinearLayout>  

 

row

Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:background="#00000000">  
  7.       
  8.     <TextView   
  9.         android:id="@+id/list_value"   
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.     />  
  13. </LinearLayout>  
  14.               
  15.