All the problems that you have encountered. But half of the content was written by himself, and half was copied. So please forgive me for being original.
1. After the ListView has added a caption (AddHeader ()), the pair that is obtained by using Listview.getadapter is the headerviewlistadapter image that was encapsulated by the header. So, in order to get the custom adapter, The method is:
Headerviewlistadapter adapter=(headerviewlistadapter) daybilllistview.getadapter ();D Ayreportadapter Dayadapter= (dayreportadapter) adapter.getwrappedadapter ();
2.item If there is a button and other controls, when listening to the Onitemclick event of the ListView, the focus will be the button, ImageButton and other controls in the item snatched, This causes the Onitemclick event to not be triggered after the ListView has been set. The workaround is to block the focus fetching of controls such as their internal buttons while initializing the item, which can be called in the root control of the custom item: setdescendantfocusability (Viewgroup.focus_block_ Descendants); Or
Add android:descendantfocusability= "Blocksdescendants" to LinearLayout and also add android:focusable= "false" to the button
This can block the word control grab the focus, the ListView Onitemclick can be triggered correctly, and the item inside the button and other controls are not affected, they can be clicked to trigger their own click events.
3. When the ListView needs to add Headerview, the method can be called by invoking the Addheaderview (Headview, NULL, False) method of the ListView, which also has an overloaded method Addheaderview ( Headview); The difference between the two methods is that the previous method can control whether the header can be selected, and if you do not want to be selected, set the third parameter to false;
4. Then add the header above said, The Addheaderview method that is called when the header is added must be placed in front of the listview.setadapter, meaning that if you want to add a header to the ListView, you must add it before binding it to adapter, or you will get an error. The reason is that when we call the Setadapter method, Android will determine if the current ListView has added a header, and if it has been added, it will generate a new headerviewlistadapter. This new headerviewlistadapter contains all the contents of the adapter we have set and the header and footer of the ListView. So when we call Listview.getadapter in the program after we add a header to the ListView, we return the tempadapter instead of the adapter we passed through Setadapter. If adapter is not set then Headerviewlistadapter is the same as our own adapter. The Listview.getadapter (). GetCount () method returns a value that is larger than we expected because the header was added.
5. The listview,onitemselected method of the Addheaderview () returned a large 1 of the position because the header was counted.
Our custom adapter inside the GetItem method inside the returned position is not included header, is our custom adapter data position number starting from 0, that is, we passed in the list of the same position.
@Override Public View GetView (int position, view Convertview, ViewGroup parent) {// TODO auto-generated Method stub// This position is the real location of our data }
And in the Onitemclick method of the ListView:
public void onitemselected (adapterview<?> parent, view view, int position, long ID)
Position is the location of the current click, which is the position in the Headerviewlistadapter, starting with 0 if a header is added to the ListView, 0 represents the header.
This article was reproduced from: http://blog.csdn.net/hutengfei0701/article/details/8494237