Android ListView Adapter

Source: Internet
Author: User

 

During this time, the ListView control is often used in projects, so I have some knowledge about the customization of the ListView control. In fact, the key to using the listView control is the use of the adapter.

 

Directly Add code

 

Package com. Adapters;

 

Import java. util. ArrayList;

Import java. util. HashMap;

 

Import com. AppClient. R;

 

 

 

Import android. content. Context;

 

Import android. view. LayoutInflater;

Import android. view. View;

Import android. view. ViewGroup;

 

Import android. widget. BaseAdapter;

 

Import android. widget. ImageView;

Import android. widget. TextView;

 

 

Public class RecipeAdapter extends BaseAdapter {

 

Private LayoutInflater layoutInflater;

Private Context context;

 

/**

* Constructor, configure Parameters

* @ Param data configure ArrayList <HashMap <String, Object> type parameters>

* @ Param context this */

Public RecipeAdapter (ArrayList <HashMap <String, Object> data,

Context context ){

Super ();

This. data = data;

This. layoutInflater = layoutInflater. from (context );

This. context = context;

}

 

Private ArrayList <HashMap <String, Object> data;

/**

* The LayoutInflater class is the main form of obtaining layout files in code implementation.

* LayoutInflater layoutInflater = LayoutInflater. from (context );

* View convertView = layoutInflater. inflate ();

* LayoutInflater is very useful in the development of the LayoutInflater class. Its function is similar to findViewById (),

The difference is that LayoutInflater is used to find the xml layout file under layout and instantiate it!

FindViewById () is used to find the widget controls (such as buttons and TextView) in a specific xml file ).

*/

 

@ Override

Public int getCount (){

// TODO Auto-generated method stub

Return data. size ();

}

 

@ Override

Public Object getItem (int position ){

// TODO Auto-generated method stub

Return data. get (position );

}

 

@ Override

Public long getItemId (int position ){

// TODO Auto-generated method stub

Return position;

}

/**

* This method is called when every column is drawn by android.

*/

@ Override

Public View getView (int position, View convertView, ViewGroup parent ){

// TODO Auto-generated method stub

Controlors controlor = null;

If (convertView = null)

{

Controlor = new Controlors ();

// Obtain the layout component

ConvertView = layoutInflater. inflate (R. layout. recipe_list, null );

Controlor. imageView = (ImageView) convertView. findViewById (R. id. recipe_image );

Controlor. textView = (TextView) convertView. findViewById (R. id. recipe_name );

Controlor. writer = (TextView) convertView. findViewById (R. id. writer );

Controlor. mainResource = (TextView) convertView. findViewById (R. id. main_resource );

Controlor. littleResourc = (TextView) convertView. findViewById (R. id. little_resource );

// Use tags to store data

ConvertView. setTag (controlor );

}

Else

{

Controlor = (Controlors) convertView. getTag ();

}

// Bind data and event trigger

Controlor. imageView. setBackgroundResource (Integer) data. get (position). get ("recipe_image "));

Controlor. textView. setText (String) data. get (position). get ("recipe_name "));

Controlor. writer. setText (String) data. get (position). get ("writer "));

Controlor. mainResource. setText (String) data. get (position). get ("main_resource "));

Controlor. littleResourc. setText (String) data. get (position). get ("little_resource "));

Return convertView;

}

 

}

 

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.