Saturday, June 30, 2012

list view control with collection framework

BeanMatrixDemo.java

package com.beanmatrix.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.*;

public class BeanMatrixDemoActivity extends Activity {
       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        ArrayList<String> al = new ArrayList<String>();
        System.out.println("Initial size of al: " +
        al.size());
        // add elements to the array list
        al.add("Milk");
        al.add("Butter");
        al.add("Yogurt");
        al.add("Toothpaste");
        al.add("IceCream");
        // Remove elements from the array list
        al.remove("Toothpaste");
        al.remove(2);
       // display the array list
         ListView listView = (ListView) findViewById(R.id.mylist);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, al);
        listView.setAdapter(adapter);

      }
}


Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

No comments:

Post a Comment