27 Dec 2012


How To Create List View In Android

List view is a ViewGroup single dimensional and scrollable view to the user.List View has some important points:
  • ListView is automatically inserted in the layout using ListAdapter(a bridge between ListView and data which backs the list).
  • android.widget.ListView is the class which needs to import for implementing the list view.
List view 1
so lets us learn how to user list view:
  • First start the eclipse and select new-> android application project.
List view 2
  • Now fill the following details in the android application project interface.
    • Application Name- ListView
    • Project Name-ListView
    • Package Name-list.view
    • and then click next ->next and then finish.
List view  3
  • Now in the project ListView open the activity_main.xml file under res-> layout-> activity_main.xml and replace the whole lines with these new lines.
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent">  
     <ListView  
       android:id="@+id/listView1"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:layout_weight="1"  
       android:background="#FFC77F" >  
     </ListView>  
   </LinearLayout>  
  • Here we have added ListView inside the Linear Layout and given background color to each ListView item.
  • Now open the MainActivity.java and add few lines of java so that it finally looks like this:
 package list.view;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.widget.ArrayAdapter;  
 import android.widget.ListView;  
 public class MainActivity extends Activity {  
       ListView listView;  
    String[] weekday={"SUN","MON","TUE","THU","FRI","SAT","SUN"};  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     listView = (ListView)findViewById(R.id.listView1);  
     ArrayAdapter<String> adapterview= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,weekday);  
     listView.setAdapter(adapterview);  
   }  
 }  
  • Now lets understand the code of the MainActicity.java:
    • import android.widget.ArrayAdapter is a concrete BaseAdapter which is backed by  arbitrary adapter.
    • import android.widget.ListView is class that is used for showing one dimensional list objects.
    • setAdapter() method sets the custom adapter for source of all item for displaying in the grid.
    • simple_list_item_1 is constant of android.R.layout which is able to change its background state to indicate when it is acticated.
  • Now our project is ready therefore we will run this project by right click on the project folder and click on run

List view  4

  • Now when you put your cursor and click on each item in the list it will change it color although it will not perform any task as we haven't assigned any function with these item.

No comments:

Post a Comment

Translate