Combination of listview, checkbox, edittext, And button

Source: Internet
Author: User

I didn't write many blogs during this time, mainly because the weather was cold and I didn't want to change my mind even if I had no Internet. I forgot my knowledge point during this time. I will review it quickly.

Today, it is simple: when listview is combined with checkbox, edittext, And button, the onlistitemclick () event cannot respond, the priority of the obtained focus of the List itself is lower than that of checkbox, edittext, And button. So set their attributes: Android: focusable = "false ". In addition, if you do not want to respond to their own onclick events, you can: Android: clickable = "false ". In this case, how does one select or not select the checkbox? One way isThe onlistitemclick () event of listview depends on a certain condition.. Here is a simple example:

For example, the layout file of my listview item is:

  

       .....
<CheckBox
android:id="@+id/selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:layout_marginRight="4dip"
android:layout_alignWithParentIfMissing="true"
android:focusable="false"
android:clickable="false"
android:visibility="gone"
/>
......

Then, in the listview onlistitemclick (), select and unselect the checkbox:

 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(isPick){
CheckBox checkbox = (CheckBox) v.findViewById(R.id.selected);
checkbox.setChecked(!checkbox.isChecked());
return;
}
// other code
}
......

Note: There are other similar methods for obtaining a control in an item of listview:

  

Relativelayout mrelativelayout = (relativelayout) V;
// The index is the index of the view in the parent view. The value starts from 0.
Checkbox = (checkbox) mrelativelayout. getchildat (INDEX );

Or you can choose to do this:

If (isselected. containskey (position )){
Isselected. Remove (position );
// Refresh the current item and check isselected in the getview function to determine the status of the checkbox.
Madapter. notifydatasetchanged ();
} Else {
Isselected. Put (Position, true );
Madapter. notifydatasetchanged ();
}

  

Now you can get what we want.

At this time, some people want to save the checkbox status (although I didn't use it), so let's talk about this problem by the way:

A feasible method is to add a hashmap variable and facilitate batch operations:

       private HashMap<Integer, Boolean> isSelected;//Batch operations

Isselected actually stores the selected status of the listview. As mentioned above, I added the isselected operation:

 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if (isPick) {
CheckBox checkbox = (CheckBox) v.findViewById(R.id.selected);
if (isSelected.containsKey(position)) {
isSelected.remove(position);
checkbox.setChecked(false);
} else {
isSelected.put(position, true);
checkbox.setChecked(true);
}
return;
}
// other code
}

Then, in the adapter, for the method getview () (or another method in BindView () called each time the view is displayed, each time an item is displayed, isselected is used to check whether the item is selected. As follows:

@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
Log. E ("adapter getview count", Position + ""
);
Viewholder holder;
If (convertview = NULL ){
Layoutinflater inflate = (layoutinflater) mcontext. getsystemservice (context. layout_inflater_service );
Convertview = inflate. Inflate (R. layout. list_item, null );
Holder = new viewholder ();
Holder. Icon = (imageview) convertview. findviewbyid (R. Id. note_icon );
Holder. Name = (textview) convertview. findviewbyid (R. Id. note_name );
Holder. Date = (textview) convertview. findviewbyid (R. Id. note_date );
Holder. checkbox = (checkbox) convertview. findviewbyid (R. Id. Selected );
Convertview. settag (holder );
} Else {
Holder = (viewholder) convertview. gettag ();
}

Holder. Icon. setimageresource (R. drawable. Icon );
Holder. Name. settext (mdata. Get (position ));
Holder. Date. settext ("date ");
If (misselected. containskey (position )){
Holder. checkbox. setchecked (true );
} Else {
Holder. checkbox. setchecked (false );
}
Return convertview;
}

Static class viewholder {
Imageview icon;
Textview name;
Textview date;
Checkbox;
}

Now, when I scroll the listview, there will be no confusion in the checkbox status of the item.

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.