ListDynamicActivity.java
package
com.android.ListDynamic;
import
java.util.ArrayList;
import android.app.Activity;
import
android.app.ListActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.ArrayAdapter;
public class ListDynamicActivity
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();
}
}
Main.xml
<?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="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/addBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"
android:text="Add New
Item" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
Output
No comments:
Post a Comment