In the last tutorial I have showed how we can implement a GridView in android now i will show how we can add a array of string in an List View without using a BaseAdapter Class . Adding a String array in ListView is considered to be a most simple way to use an ListView. List View is easy and attractive way to display the data Now days you will see that the most of the application uses the list view and by this the application also look attractive. So lets start.
First Step :-
Take a list view in your activity_main XML file like this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
Second Step:-
Now create another XMl file and put an TextView in it. Name the file as list_item.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" >
<!-- Single ListItem -->
<!-- Product Name -->
<TextView android:id="@+id/product_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
Third Step:-
Now comes to source code open the MainActivity and the create a string array, ListView and ArrayAdapter.
private final String[] Itdelhi = new String[]
{"ItDevelopersDelhi","Android","SEO","Wed Development" };
ArrayAdapter<String> adapter;
ListView lv;
put the following code in oncreate function.
lv = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, Itdelhi);
lv.setAdapter(adapter);
Now we are ready to execute the code.
Snapshots:-
First Step :-
Take a list view in your activity_main XML file like this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
</RelativeLayout>
Second Step:-
Now create another XMl file and put an TextView in it. Name the file as list_item.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" >
<!-- Single ListItem -->
<!-- Product Name -->
<TextView android:id="@+id/product_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
Third Step:-
Now comes to source code open the MainActivity and the create a string array, ListView and ArrayAdapter.
private final String[] Itdelhi = new String[]
{"ItDevelopersDelhi","Android","SEO","Wed Development" };
ArrayAdapter<String> adapter;
ListView lv;
put the following code in oncreate function.
lv = (ListView) findViewById(R.id.list);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, Itdelhi);
lv.setAdapter(adapter);
Now we are ready to execute the code.
Snapshots:-
Click to download the project
No comments:
Post a Comment