Android learning diary 18 -- Adapter Introduction

Source: Internet
Author: User

I. Adapter 1. Briefly describe that the recently learned GridView and Gallery both use the Adapter and find it very important. I searched for information about the Adapter on the Internet. The vast majority of android applications are written in JAVA. Adapter is a special design pattern in JAVA to combine two unrelated classes. For example, if the pipe A is 25mm, and the pipe B is 40mm, assuming that the pipe thickness is not counted, how to connect the two kinds of pipe for use? In this case, you need to use the adapter C, which is similar to A water pipe. One end is 25mm, and the other is 40mm. In this way, you can connect A and B water pipes for use. 2. the Adapter in Android is a bridge between the View and data. The Adapter provides access to the data and generates a corresponding View for each data item. It is the relationship between Data, Adapter, and View: Andpter class structure system: Among these adapters, BaseAdapter seems to be quite common due to its flexible use, you can bind a cursor to display data. 3. No matter which type of Adapter is rewritten, the following four methods must be rewritten: 1 // The number of items in the data indicated by 2 int getCount (); 3 4 // return the data item at the specified position 5 Object getItem (int position); 6 7 // return the ID of the data item at the specified position 8 long getItemId (int position ); 9 10 // generate the corresponding View for each data item 11 View getView (int position, View convertView, ViewGroup parent); Some netizens found that getItem (), and getItemId () these two methods are useless for android, simply for the convenience of client calls. In the previous article, I overwrote getItem () and getItemId () to set the initial position. 4. simple example: for the extended BaseAdapter custom code, see the ListView, GridView, and Gallery articles. The following code mainly posts how to construct a SimpleCursorAdapter to display the contact information: 1 listView = new ListView (this); 2 // Table 3 Cursor cursor = getContentResolver () in the system (). query (People. CONTENT_URI, null, 4 null, null, null); 5 6 startManagingCursor (cursor); 7 8 ListAdapter listAdapter = new SimpleCursorAdapter (this, // Context 9 android. r. layout. simple_expandable_list_item_1, // The style 10 11 cursor for ListView to be displayed, // data source 12 13 new String [] {People. NAME}, // corresponding field 14 15 new int [] {android. r. id. text1}); // display the control on which 16 17 listView is displayed. setAdapter (listAdapter); 18 19 setContentView (listView );

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: 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.