Dynamic add Lsitview code instance in Android development

Source: Internet
Author: User
Tags xmlns

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.