Android Simple ListView Tutorial
Last Updated: September 22, 2012
In this tutorial, you will learn how to create a simple ListView in your Android application. The ListView is a view group that displays a list of vertical scrollable items. We will create a ListView that allows users to scroll the list, and on ListView item click will show results on a new activity using an intent method. So lets begin…
Create a new project in Eclipse, select File > New > Android Application Project. Fill in the details and name your project ListViewTutorial.
Application Name : ListViewTutorial
Project Name : ListViewTutorial
Package Name : com.androidbegin.listviewtutorial
Open your MainActivity.java and paste the following code.
MainActivity.java
package com.androidbegin.listviewtutorial; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.view.View; public class MainActivity extends Activity { // Declare variables ListView listView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from listview_main.xml setContentView(R.layout.listview_main); // Store string resources into an Array String[] SamsungPhones = new String[] { "Galaxy S", "Galaxy S2", "Galaxy Note", "Galaxy Beam", "Galaxy Ace Plus", "Galaxy S3", "Galaxy S Advance", "Galaxy Wave 3", "Galaxy Wave Y", "Galaxy Nexus", "Galaxy W", "Galaxy Y", "Galaxy Mini", "Galaxy Gio", "Galaxy Wave", "Galaxy Wave 2" }; // Locate ListView in listview_main.xml listView = (ListView) findViewById(R.id.listview); // Bind array strings into an adapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, SamsungPhones); listView.setAdapter(adapter); // Capture ListView item click listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Capture the click position and set it into a string String phones = (String) listView.getItemAtPosition(position); // Launch SingleItemView.java using intent Intent i = new Intent(MainActivity.this, SingleItemView.class); // Send captured string to SingleItemView.java i.putExtra("phones", phones); // Start SingleItemView.java startActivity(i); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_list_view, menu); return true; } }
Strings are stored in a string array and set into a ListView using an adapter. We used an onitemclicklistener to capture ListView item click position and pass the selected string to a new activity called SingleItemView.java.
Next, create an XML file for your MainActivity graphical layout. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file listview_main.xml and paste the following code.
listview_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
Output:
Next, create a new activity in a new class called SingleItemView.java. Go to File > New > Class and name it SingleItemView.java. Select your package named com.androidbegin.listviewtutorial and click Finish.
Open your SingleItemView.java and paste the following code.
SingleItemView.java
package com.androidbegin.listviewtutorial; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SingleItemView extends Activity { // Declare variable String phones; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from single_item_view.xml setContentView(R.layout.single_item_view); // Locate the TextView in single_item_view.xml TextView txtPhones = (TextView) findViewById(R.id.text); // Get string from intent passed from MainActivity.java Intent i = getIntent(); phones = i.getStringExtra("phones"); // Set the string into TextView txtPhones.setText(phones); } }
An intent is used to retrieve a string from the previous activity and set the string into a TextView.
Next, create an XML file for your SingleItemView graphical layout. Go to res > layout > Right Click on layout > New > Android XML File
Name your new XML file singleitemview.xml and paste the following code.
singleitemview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="30dip" /> </RelativeLayout>
Next, change the application name. Open your strings.xml in your res > values folder and paste the following code.
strings.xml
<resources> <string name="app_name">ListView Tutorial</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> </resources>
In your AndroidManifest.xml, we need to declare an activity for SingleItemView.java. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidbegin.listviewtutorial" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SingleItemView" > </activity> </application> </manifest>
Output:
Source Code
[purchase_link id=”7814″ text=”Purchase to Download Source Code” style=”button” color=”green”]
Same Exact we need frm json or mysql DB
M Hussain Afridi
Android Simple ListView Tutorial
Great example. Thanks!
leeseawuyhs
Android Simple ListView Tutorial