The project describes how to implement list view for your application.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project ListViewExample
2.) Open and insert following in main.xml:
2.) Open and insert following in main.xml:
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01"android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01"android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ListViewExample. Enter following information:
Project name: ListViewExample
Build Target: Android 2.3.3
Application name: ListViewExample
Package name: org.example.ListViewExample
Create Activity: ListViewExample
Build Target: Android 2.3.3
Application name: ListViewExample
Package name: org.example.ListViewExample
Create Activity: ListViewExample
On Clicking Finish ListViewExample code structure is generated with the necessary Android Packages being imported along with ListViewExample.java. ListViewExample class will look like following:
package org.example.ListViewExample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewExample extends Activity {
private ListView lv1;
private String lv_arr[]={"Item1","Item2","Item3","Item4"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(newArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
}
}
private ListView lv1;
private String lv_arr[]={"Item1","Item2","Item3","Item4"};
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(newArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
}
}
Output – The final output:
No comments:
Post a Comment