Pages

Wednesday, May 19, 2010

Use Of List View in android

ListView is a frequently used controls, ListView inside of each sub-item Item can be a string, it can be a combination of controls. First talk about the implementation of ListView: 

1. Preparation ListView to display data; 

2. Using one-dimensional or multidimensional dynamic arrays stored data; 

2. Construction of the adapter, simply, the adapter is the Item array, dynamic array how manyelements to generate the number of Item; 

3. The adapter to the ListView, and displayed. 

Then began the XML UI code: 

***********************
main.xml code below is very simple and does not need more explanation: 
(i am using () in place of <> due to blog post restriction.)
- - - - - - - - - - - - - - - - - - - - - - - - 
(?xml version="1.0" encoding="utf-8"?)
(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:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:id="@+id/MyListView")
        (/ListView)
(/LinearLayout)
***********************
create a new "my_listitem.xml" the code below, my_listitem.xml ListView for the design of the Item:


***********************
(?xml version="1.0" encoding="utf-8"?)

(LinearLayout    
        android:layout_width="fill_parent"    
        xmlns:android="http://schemas.android.com/apk/res/android"    
        android:orientation="vertical"  
        android:layout_height="wrap_content"    
        android:id="@+id/MyListItem"    
        android:paddingBottom="3dip"    
        android:paddingLeft="10dip")
       (TextView    
                android:layout_height="wrap_content"    
                android:layout_width="fill_parent"    
                android:id="@+id/ItemTitle"    
                android:textSize="30dip")
        (/TextView)
        (TextView    
                android:layout_height="wrap_content"    
                android:layout_width="fill_parent"    
                android:id="@+id/ItemText")
        (/TextView) 
(/LinearLayout)

***********************
Explanation, which used some of the attributes: 

1.paddingBottom = "3dip", Layout to stay out of the bottom 3 pixels of white space 

2.paddingLeft = "10dip", Layout to the left leaving a blank area of 10 pixels 

3.textSize = "30dip", TextView font is 30 pixels so great. 

Last is the JAVA source code: 

Create a activity class called MyListViewItems.java. embed following oncreate() method in it.

*************************


public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);   
    // Bound in XML  ListView, As the container for the Item     
    ListView list = (ListView) findViewById(R.id.MyListView);   
       
    // Build a dynamic array, and reprint data     
    ArrayList> mylist = new ArrayList>();   
    for(int i=0;i<30;i++)   
    {   
        HashMap map = new HashMap();   
        map.put("ItemTitle", "This is Title.....");   
        map.put("ItemText", "This is text.....");   
        mylist.add(map);   
    }   
    // Generated adapter, array  ===>ListItem   
    SimpleAdapter mSchedule = new SimpleAdapter(this, // There's nothing to explain     
                                                mylist,// Data source      
                                                R.layout.my_listitem,//ListItem XML implementation     
                                                   
                                                // Dynamic arrays and the subkey that corresponds to the ListItem             
                                                new String[] {"ItemTitle", "ItemText"},    
                                                   
                                                //ListItem The XML file inside of two  TextView ID   
                                                new int[] {R.id.ItemTitle,R.id.ItemText});   
    // Add and display     
    list.setAdapter(mSchedule);   
}  
*************************

Basic steps: 
1. Binding XML in a ListView 
Such as: ListView list = (ListView) findViewById (R.id.ListView); 

2. Generate dynamic array, adding data 
ArrayList > listItem = new ArrayList > (); 
. . . . . . 
Last listItem.add (map); 

3. Generated adapter Item and dynamic arrays corresponding elements such as: SimpleAdapter listItemAdapter = new SimpleAdapter (this, listItem, / / data source 
R.layout.list_items, / / ListItem's XML Implementation 
/ / Dynamic array and ImageItem corresponding subkey new String [] ("ItemImage", "ItemTitle", "ItemText"), 
/ / ImageItem the XML file inside a ImageView, two TextView ID new int [] (R.id.ItemImage, R.id.ItemTitle, R.id.ItemText)); 

Four. Add and display, such as: list.setAdapter (listItemAdapter); 

These four steps as necessary 

5. Add click 
list.setOnItemClickListener (new OnItemClickListener () ( 

@ Override 
public void onItemClick (AdapterView arg0, View arg1, int arg2, 
long arg3) ( 
setTitle ("Click on the first" + arg2 + "items"); 

)); 

6. Add the long-per-click 
list.setOnCreateContextMenuListener (new OnCreateContextMenuListener () ( 

@ Override 
public void onCreateContextMenu (ContextMenu menu, View v, 
ContextMenuInfo menuInfo) ( 
menu.setHeaderTitle ("long press the menu-ContextMenu"); 
menu.add (0, 0, 0, "pop-up menu, long press 0"); 
menu.add (0, 1, 0, "pop-up menu long by 1"); 

)); 

7. Long response function according to the menu 
@ Override 
public boolean onContextItemSelected (MenuItem item) ( 
setTitle ("Click the long press the menu inside the first" + item.getItemId () + "items"); 
return super.onContextItemSelected (item); 
)

1 comment: