Summary about how to bind data to the check box through listview
Source:
1. Select the check boxes 2 and 3 on the first page. When you flip to the second page, the check boxes are also selected at the same position.
Solution: You must not delete the blacklist number by selecting the check box (for the reason, see the analysis above), but by which data is selected to delete the blacklist. so how do I know if this data is selected? Is through
The checkbox is selected to prompt the user. So checkbox only plays a role in display !!
Solution: Customize the simpleadapter class and override the getview () method. In addition, the external global balcknumberischeckedmap is used to save the status of each phone number, which is not selected at the beginning.
Purpose of rewriting the getview () method:
(1) implement data binding checkbox. settext (blacknumber); // here is the essence of the binding. Bind the phone number to the checkbox of listview.
(2) Assign the initial status to the checkbox (the initial status is not selected)
Boolean ischecked = balcknumberischeckedmap. Get (ID );
Checkbox. setchecked (ischecked );
2. By listening to the entry click event, we can know that the data behind the entry is clicked
That is, hashmap <string, Object> itemhashmap = (hashmap <string, Object>) LV. getitematposition (position); then you can modify the status of this number in balcknumberischeckedmap.
This status is used to determine the status of the checkbox check box. In this way, you can click several entries to traverse the balcknumberischeckedmap to check which numbers are selected and delete the selected number.
This leads to the second problem: When you click an entry, the entry is not actually clicked, but the check box checkbox is clicked, because the check box has a high priority.
Solution: In the layout file, the check box cannot be clicked, touched, or focused !!! So when you click an entry, you are actually clicking an entry !!!!!!!!
You can process the entry click event as follows:
(1) switch the checkbox. Toggle () status when you click the check box. This status is changed for each click entry. Then, the current status of the check box is Boolean itemischeck = checkbox. ischecked ();
(2) obtain the currently clicked phone number hashmap <string, Object> itemhashmap = (hashmap <string, Object>) LV. getitematposition (position );
(3) obtain the number ID, that is, int blacknumberid = (integer) itemhashmap. Get ("ID ");
(4) then modify the status of this number in the balcknumberischeckedmap, that is, balcknumberischeckedmap. Put (blacknumberid, itemischeck );
In this way, the form and content are unified, the status of the check box changes, and the actual situation is correctly reflected.
Core Code As follows:
Checkbox. Toggle ();
Boolean itemischeck = checkbox. ischecked (); // get the current status of the checkbox
Hashmap <string, Object> itemhashmap = (hashmap <string, Object>) LV. getitematposition (position); // obtain the hashmap bound to the clicked entry of listview.
Int blacknumberid = (integer) itemhashmap. Get ("ID ");
Balcknumberischeckedmap. Put (blacknumberid, itemischeck );
When we perform the deletion, we actually go to hashmap <integer, Boolean> balcknumberischeckedmap to check which data is selected. If we select the data, we will delete it.
This summary is derived from the small project-called Genie