In this tutorial i will show how to create a simple listview application. This article is about creating listview.
1. So now we are creating a project in Eclipse IDE.
1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as ListviewActivity.
2. Once the project is created open your main activity java file and extend the class from ListActivity.
public class ListviewActivity extends ListActivity
2. Now we need a string for display your list item. So now we are implementing array concept. Here the following code look like.
String args[]={"TamilNadu","Bombay","Kerala","Andra","Karnataka","Mangloor","Goa","Delhi"};
3. In ListView each list item will be an xml layout, so we can customize each list item. Create an XML file under res/layout folder and name it as main.xml and type the following code. This xml layout will be single list item row.
( Right Click on res/layout ⇒ New ⇒ Android XML File)
<?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" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
4. Now open your main activity java file and type the following code. In the following code i am importing all xml resources data and storing them in an Array. On the next step i am binding array to ListAdapter.
public class ListviewActivity extends ListActivity
{
String args[]={"TamilNadu","Bombay","Kerala","Andra","Karnataka","Mangloor","Goa","Delhi"};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,args);
setListAdapter(ad);
}
public void onListItem(ListView lv,View v,int pos,long id)
{
super.onListItemClick(lv,v,pos,id);
Toast.makeText(getBaseContext(), "you selected at="+args[pos], Toast.LENGTH_LONG).show();
}
}
5. The final step is to add an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.listview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ListviewActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Categories:
Post a Comment