This is easy to understand. Do not forget adapter. notifyDataSetChanged ();
Wake up. In fact, this wake-up function is used under the button or called under another control. If it is called under getView, it is no longer necessary.
Java code:
Public class DynamicListItems extends ListActivity {
Private static final String ITEM_KEY = "key ";
ArrayList <HashMap <String, String> list = new ArrayList <HashMap <String, String> ();
Private SimpleAdapter adapter;
Private EditText newValue;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. dynamic_list );
NewValue = (EditText) findViewById (R. id. new_value_field );
SetListAdapter (new SimpleAdapter (this, list, R. layout. row, new String [] {ITEM_KEY}, new int [] {R. id. list_value }));
(ImageButton) findViewById (R. id. button). setOnClickListener (getBtnClickListener ());
}
Private OnClickListener getBtnClickListener (){
Return new OnClickListener (){
Public void onClick (View view ){
Try {
HashMap <String, String> item = new HashMap <String, String> ();
Item. put (ITEM_KEY, newValue. getText (). toString ());
List. add (item );
Adapter. notifyDataSetChanged ();
} Catch (NullPointerException e ){
Log. I ("[Dynamic Items]", "Tried to add null value ");
}
}
};
}
}
Java code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<ListView android: id = "@ + id/android: list"
Android: layout_width = "fill_parent"
Android: layout_height = "0px"
Android: layout_weight = "1"
Android: cacheColorHint = "#00000000">
</ListView>
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "6px"
Android: layout_marginBottom = "8px"
Android: layout_marginLeft = "8px"
Android: layout_marginRight = "8px"
Android: background = "#00000000">
<ImageButton
Android: id = "@ + id/button"
Android: layout_width = "100px"
Android: layout_height = "100px"
Android: src = "@ android: drawable/ic_menu_add"/>
</LinearLayout>
<LinearLayout
Android: orientation = "vertical"
Android: layout_width = "230px"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "8px"
Android: layout_marginBottom = "8px"
Android: layout_marginLeft = "0px"
Android: layout_marginRight = "28px"
Android: background = "# cccccc00">
<EditText android: id = "@ + id/new_value_field"
Android: textColor = "#020905"
Android: textSize = "18sp"
Android: layout_marginTop = "6px"
Android: layout_marginBottom = "8px"
Android: layout_marginLeft = "12px"
Android: layout_marginRight = "12px"
Android: layout_width = "210px"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"/>
</LinearLayout>
</LinearLayout>
ROw:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "#00000000">
<TextView
Android: id = "@ + id/list_value"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</LinearLayout>