Wednesday 17 October 2012

ANDROID - ListView Example 1






















ListViewExample_1.java:


package com.venkool;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListViewExample_1Activity extends Activity {
/** Called when the activity is first created. */

ListView lvList;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String[] str = getResources().getStringArray(R.array.names);

lvList = (ListView) findViewById(R.id.lvList);

ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, str);

lvList.setAdapter(adapter);
}
}



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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Data comes under Strings.xml file"
        android:textSize="15dp" />
 
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/lvList">
     
    </ListView>

</LinearLayout>


strings.xml:


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, ListViewExample_1Activity!</string>
    <string name="app_name">ListViewExample_1</string>
 
        <string-array name="names">
            <item>Hyderabad</item>
            <item>Bangalore</item>
            <item>Chennai</item>
            <item>Delhi</item>
            <item>India</item>
            <item>Srilanka</item>
            <item>USA</item>
            <item>UK</item>
        </string-array>

</resources>


No comments:

Post a Comment