Custom GridView/ListView Data Source

Source: Internet
Author: User

During development, we often encounter complicated layout of the GridView/ListView. The re-implementation of BaseAdapter not only helps us achieve the layout we want, in addition, when binding a large amount of data, it does not feel stuck. Remember to use a ListView to directly bind the contact Cursor in the mobile phone (more than one hundred people), sliding will feel stuck. I decided to write a Demo today because the project may achieve this effect:Bind four ImageButton to a GridView. Some buttons are unavailable under certain circumstances, that is, Enable = false. Different buttons must have different click events..

First, add a flag boolean bl to control the Enable status of the button. Second, add View. onClickListener to the List.

1. MyAdapter. java inherits from BaseAdapter

Public ClassMyAdapterExtendsBaseAdapter {

PrivateContext context;
PrivateList<Map<String, Object>List;
PrivateLayoutInflater mInflater;
PublicMyAdapter (Context context, List<Map<String, Object>List ){
This. Context=Context;
This. List=List;
MInflater=LayoutInflater. from (This. Context );
}
Public IntGetCount (){
//TODO Auto-generated method stub
If(List! =Null)
ReturnList. size ();
Else
Return 0;
}

PublicObject getItem (IntPosition ){
//TODO Auto-generated method stub
If(List! =Null)
ReturnList. get (position );
Else
Return Null;
}

Public LongGetItemId (IntPosition ){
//TODO Auto-generated method stub
ReturnPosition;
}

@ SuppressWarnings ("Unused")
PublicView getView (IntPosition, View convertView, ViewGroup parent ){
//TODO Auto-generated method stub
ViewHolder holder=Null;
If(Holder=Null){
Holder=NewViewHolder ();
ConvertView=MInflater. inflate (R. layout. gridview_item,Null);
Holder. ib=(ImageButton) convertView. findViewById (R. id. ib );
ConvertView. setTag (holder );
}Else{
Holder=(ViewHolder) convertView. getTag ();
}
//Bind a click event
Holder. ib. setOnClickListener (OnClickListener) list. get (position). get ("Listen"));
//Enable with flag control button
If(Boolean. parseBoolean (list. get (position). get ("Bl"). ToString ()))
Holder. ib. setEnabled (True);
Else
H

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.