Android Development ListView, GridView Detailed and sample code _android

Source: Internet
Author: User
Tags event listener

ListView and GridView are common controls in Android development, and they work with adapter to achieve many interface effects. The following examples illustrate the use of ListView and GridView respectively.

1.ListView Example of Android development

ListView is one of the most commonly used controls in Android development, and the general composition list consists of three elements, ListView: A view of the list, a Adapter: A bridge for data to view, and data: A string, a picture, or a control.

Adapters generally have the following types:

The simplest type of adapter in the Arrayadapter:android, specifically for list controls. Only one row of data is displayed.

Simpleadapter: This adapter has the best extensibility and can be customized for a variety of effects. You often populate lists with static data.

CursorAdapter: Provides data to the list through cursors.

Resourcecursoradapter: This adapter extends CursorAdapter and knows how to create a view from a resource.

Simplecursoradapter: This adapter extends resourcecursoradapter to create a Textview/imageview view from a column in a cursor. Here's an example of an address book:

xml/html Code

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=   
"http://schemas.android.com/apk/" Res/android "   
  android:orientation=" vertical "   
  android:layout_width=" fill_parent "   
  android:layout_" height= "Fill_parent"   
  android:background= "@drawable/bg"   
     
  >   
  <listview android:id=    
  "@+id/ Contacts_list "   
  android:layout_width=" fill_parent "   
  android:layout_height=" fill_parent "   
  >   
  </ListView>   
</LinearLayout>

Java code

Package net.csdn.blog.androidtoast;   
Import java.util.ArrayList;   
Import Java.util.HashMap;   
   
Import Java.util.Map;   
Import android.app.Activity;   
Import Android.database.Cursor;   
Import Android.os.Bundle;   
Import Android.provider.ContactsContract;   
Import Android.view.View;   
Import Android.widget.AdapterView;   
Import Android.widget.ListAdapter;   
Import Android.widget.ListView;   
Import Android.widget.SimpleAdapter;   
   
Import Android.widget.Toast;   
   public class Mainactivity extends activity {ListView Mlistview;   
        
  Arraylist<map<string, string>> Listdata;   
  Static final String name = "Name";   
  Static final String number = "Number"; /** called the activity is a.   
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   
    Setcontentview (R.layout.main);   
  Getcontacts (); /** * Get Contact list * * private void Getcontacts () {mlistview= (ListView) Findviewbyid (r.id.contacts_list);   
    Listdata = new arraylist<map<string, string>> ();    
    Gets the database Cursor Cursor cur=getcontentresolver (). query (ContactsContract.Contacts.CONTENT_URI, NULL, NULL, NULL, NULL);   
    Startmanagingcursor (cur);   
     while (Cur.movetonext ()) {map<string, string> MP = new hashmap<string, string> ();   
     Long id = cur.getlong (cur.getcolumnindex ("_id")); Cursor pcur = Getcontentresolver (). query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, Contactscon Tract.   
            
     CommonDataKinds.Phone.CONTACT_ID + "=" + long.tostring (ID), NULL, NULL);   
     Handling multiple numbers String phonenumbers = ""; while (Pcur.movetonext ()) {String Strphonenumber = pcur.getstring (Pcur.getcolumnindex (CONTACTSC Ontract.   
           CommonDataKinds.Phone.NUMBER));   
     Phonenumbers + = Strphonenumber + ":";   
 }       Phonenumbers + = "\ n";   
               
        Pcur.close ();   
        String name = cur.getstring (Cur.getcolumnindex ("display_name"));   
        Mp.put (name, name);   
        Mp.put (number, phonenumbers);   
    Listdata.add (MP);   
        
    } cur.close (); Set up an adapter to query the data listadapter adapter = new Simpleadapter (this, Listdata, Android. R.layout.simple_list_item_2, new string[]{name, number}, new int[] {Android. R.id.text1, Android.   
    R.ID.TEXT2});   
    Mlistview.setadapter (adapter);   
      Add Event Listener Mlistview.setonitemselectedlistener to ListView (new Listview.onitemselectedlistener () {@Override   
        public void onitemselected (adapterview<?> parent, view view, int position, long ID) {   
      Toast.maketext (Getapplicationcontext (), "Current Behavior:" +long.tostring (Parent.getselecteditemid () +1), 1). Show (); @Override public void onnothingselected (adapterview<?>Parent) {//TODO auto-generated Method stub}});  
 }   
}

2.GridView Example of Android development

The GridView grid view, which is used to display multiple rows and columns. Directly on the example:

xml/html Code

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_pare NT "android:background=" @drawable/bg "> <gridview android:id=" @+id/gridview "Android:la Yout_width= "Fill_parent" android:layout_height= "Wrap_content" android:numcolumns= "3"/> </Li Nearlayout> xml/html Code <?xml version= 1.0 "encoding=" Utf-8 "?> <linearlayout xmlns:android=" Http://sch Emas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" fill_parent "Android:layo"     
        ut_height= "fill_parent" android:scrollbars= "vertical" > <imageview android:layout_height= "100dip" Android:id= "@+id/itemimage" android:layout_width= "80dip" android:src= "@drawable/png1 "Android:layout_gravity= "Center_horizontal"/> <textview android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:id= "@+id/itemt  
 Ext "/> </LinearLayout>

Java Code

Package net.csdn.blog.androidtoast;   
Import java.util.ArrayList;   
   
Import Java.util.HashMap;   
Import android.app.Activity;   
Import Android.os.Bundle;   
Import android.view.Gravity;   
Import Android.view.View;   
Import Android.widget.AdapterView;   
Import Android.widget.GridView;   
Import Android.widget.SimpleAdapter;   
   
Import Android.widget.Toast; The public class Mainactivity extends activity {/** called the ' when the ' is ' the activity ' is the ' the './//define picture integer array p Rivate int[] mimages={r.drawable.png1, R.drawable.png2, R.drawable.png3, R.DRAWABLE.P Ng4, R.drawable.png5, R.drawable.png6, R.drawable.png7, R.drawable.png8, r.dr   
  AWABLE.PNG9};   
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);   
       
    Setcontentview (R.layout.main); Instantiating the GridView GridView mgridview= (GridView) Findviewbyid (R. Id.gridview); Generates a dynamic array and passes in the data arraylist 

Look at these two examples of Android development, I believe that the ListView, the use of the GridView has a certain grasp. You can also use them and adapter to implement more features to try.

The above is the Android ListView and GridView data collation, follow-up to continue to supplement the relevant information, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.