+ Reply to Thread
Results 1 to 2 of 2

Thread: Add dynamically elements to a listView Android

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    8

    Add dynamically elements to a listView Android

    Can anyone explain me or suggest me a tutorial where I find how can I create a listView in android, that let me to add dynamically new elements with the pressing of a button, but with the simplest procedure (then not with improvement of performance or convertView, if possible)

    I know that there are already some posts about this question, but I haven't found anything that allow me to understands it well. Thanks

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    15
    Create a xml layout first in your projects res/layout/main.xml folder
    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" >
    <Button
    android:id="@+id/addBtn"
    android:text="Add New Item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="addItems"/>
    <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    />
    </LinearLayout>
    This is a simple layout with button on top and a list view below. note list view has id @android:id/list which defines default listview a ListActivity can use.
    
    public class ListViewDemo extends ListActivity {
    
    
    //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
    ArrayList<String> listItems=new ArrayList<String>();
    
    //DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
    ArrayAdapter<String> adapter;
    
    //RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
    int clickCounter=0;
    
    @Override
    public void onCreate(Bundle icicle) {
    
    super.onCreate(icicle);
    setContentView(R.layout.main);
    adapter=new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1,
        listItems);
    setListAdapter(adapter);
    }
    
    //METHOD WHICH WILL HANDLE DYNAMIC INSERTION
    public void addItems(View v) {
     listItems.add("Clicked : "+clickCounter++);
     adapter.notifyDataSetChanged();
    }
    }
    android.R.layout.simple_list_item_1 is default list items layout supplied by android and you can use this stock layout for non complex things.

    listItems is an array list which holds the data shown in the ListView and all the insertion and removal should be done on listItems the changes in list should reflect in the view and thats handled by ArrayAdapter<String> adapter which should be notified using
    Code:
    adapter.notifyDataSetChanged();
    Adapter is instantiated with 3 paramters the context which could be your activity/listactivity the layout of you individual list item and lastly the list which is the actual data to be displayed in the list.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts