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;
}
}