Combination of ListView and Adapter rewriting, listviewadapter
Use of ListView and Adapter
First, we will introduce that ListView is one of the more common components in the Android development process. It presents data in the form of a list. Generally, a ListView consists of the following three elements:
1. View: used to display the list. It is usually specified by an xml file. As we all know, the Android interface is basically completed by the xml file, so the ListView interface should also use the xml definition. For example, the commonly used "android. R. layout. simple_list_item" in ListView is an xml file defined in the Android system.
2. the adapter is used to map different data to the View. Different data maps to different adapters, such as BaseAdapter, ArrayAdapter, CursorAdapter, and SimpleAdapter. They can Map data such as arrays, pointers, and maps to views. It is precisely because of the existence of the adapter that the use of ListView is quite flexible. After the adapter is processed, it seems that all data is mapped to the same view.
3. Data, specifically ing data and resources, can be strings, images, etc. Through the adapter, the data will be implemented on the ListView. All data and resources must be displayed in the ListView using an adapter.
The existing adapters of the system can display basic data to ListView, such as array, data pointed by Cursor, and data in Map. However, in actual development, the adapters implemented by these systems sometimes cannot meet our needs. In addition, the system's built-in multi-choice function ListView has some problems in actual use. To implement a complex ListView, You can inherit the ListView and rewrite the corresponding method, or inherit the BaseAdapter to implement it.
The relationship between the Adapter and ListView is as follows:
Next, we will introduce how to combine ListView and Adapter.
1. First declare ListView in the activity_main.xml File
<ListView
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: id = "@ + id/lv_left_menu"
Android: divider = "@ null">
</ListView>
------------------------------------------------------------ Sad split line --------------------------------------------------------------------------------------
Then bind the component to mainActivity. java.
Private ListView listView;
Private Adapter adapter; // Adapter
Private ArrayList <String> arrayList; // stores the data of each item as a data structure.
Statement of objects above
Bind listView = (ListView) findViewById (R. id. lv_left_menu );
Objects and above are bound to objects.
ArrayList = new ArrayList <> ();
Assume that four items are stored in the arrayList.
For (int I = 0; I <4; I ++)
ArrayList. add ("item" + I );
The above is the data initialization.
Adapter = new Adapter (this, android. R. layout. simple_list_item1, arrayList );
ListView. setAdapter (adapter); // The formal binding of the adapter and ListView
Note: android. R. layout. simple_list_item1 is a simple item layout file provided by the system,
Later, we can bind the item layout file written by ourselves in the Custom Adapter.
The secret and above are the bindings of the adapter and listView.
The simple ListView is completed as described above, but the item layout file provided by the system is usually android. r. layout. simple_list_item1 is not available in development. In this case, we need to rewrite the Adapter to bind custom xml files and present a variety of item la S.
When ListView starts to draw, the system first calls the getCount () function, obtains the length of the listView based on its return value, and then calls getView () to draw each row one by one based on the length. If the returned value of getCount () is 0, the return 1 is not displayed in the list, and only one row is displayed.
When the system displays the list, an adapter is first instantiated (the custom adapter will be instantiated here ). When manual adaptation is completed, data must be manually mapped. You need to override the getView () method. This method is called when every row in the list is drawn. GetView () has three parameters. position indicates the row to be displayed, and covertView indicates the layout from the layout file. We use the LayoutInflater method to extract the defined item. xml file into a View instance for display. Then, instantiate each component in the xml file (simple findViewById () method ). In this way, the data can be mapped to each component. However, to respond to a click event, you need to add a click listener for the button to capture the click event. So far, a custom listView is complete. Now let's look back at this process. The system wants to draw the ListView. He first obtains the length of the list to be drawn, and then begins to draw the first line. How can he draw it? Call the getView () function. In this function, first obtain a View (actually a ViewGroup), then instance and set each component to display it. Now, we have drawn this line. Draw the next row until the painting is complete.
Here we mainly rewrite the four methods of BaseAdapter
1. getCount (); // get the number of items
2. getItem () // obtain the object in arrayList, which is stored in String type
3. getItemId () // obtain the ID number of each item, from 0 ----- n-1
4. getView () // The method used to draw an item. Therefore, we mainly draw items here, including binding the imageView component and TextView component and obtaining the drawable image, or setText
Public class MyAdapter extends BaseAdapter {
Private Context context;
Private ArrayList <String> arrayList;
Private TextView textView;
Private ImageView imageView;
Public MyAdapter (Context context, ArrayList <String> list ){
This. arrayList = list;
This. context = context;
}
@ Override
Public int getCount (){
Return arrayList. size ();
}
@ Override
Public String getItem (int position ){
Return arrayList. get (position );
}
@ Override
Public long getItemId (int position ){
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
LayoutInflater layoutInflater = LayoutInflater. from (this. context); // obtain the view of the main interface
View view = layoutInflater. inflate (R. layout. drawer_list_item, null); // bind your own R. layout. drawer_list_item.xml File
// String str = arrayList. get (position );
TextView = (TextView) view. findViewById (R. id. text_item );
ImageView = (ImageView) view. findViewById (R. id. img_item );
// ImageView. getResource (). getDrawable (R. drawable. bell); this method is not available. imageView. setImageResource (Int ResId) should be used)
ImageView. setImageResource (R. drawable. bell );
TextView. setText ("hello" + position );
Return view;
}
// The following function adds different content for different items
// Private void init_Image_Text (int position ){
// Switch (position ){
// Case 0:
// ImageView. getResources (). getDrawable (R. drawable. bell );
// TextView. setText ("User Name ");
// Break;
// Case 1:
// ImageView. getResources (). getDrawable (R. drawable. bell );
// TextView. setText ("item" + position );
//}
//}
}
The custom Adapter above is inherited from the BaseAdapter class.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "horizontal" android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ImageView
Android: layout_gravity = "center"
Android: id = "@ + id/img_item"
Android: src = "@ drawable/bell"
Android: layout_height = "wrap_content"
Android: layout_width = "wrap_content"
Android: layout_margin = "10dp"/>
<TextView
Android: layout_margin = "10dp"
Android: textSize = "25dp"
Android: textColor = "@ color/black"
Android: layout_gravity = "center_vertical"
Android: id = "@ + id/text_item"
Android: text = "@ string/app_name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
Above is the drawer_list_item.xml file I wrote.
The above steps inherit the Adapter and use MyAdapter to implement custom ListView.
--------------------------------------------------- The perfect regression line for Cc ---------------------------------------------------------------------------------------------------------------