This is very simple to explain don't forget adapter.notifydatasetchanged ();
Wake up, in fact, this wake-up call is used under the button or in other controls down use if it's under the GetView, it's not necessary.
Java code:
public class Dynamiclistitems extends Listactivity {
private static final String Item_key = "KEY";
arraylist
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 (this, list, R.layout.row, new string[] {Item_key}, new int[] {r.id.list_value}) (new Simpleadapter);
((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:d rawable/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>