How to Use BaseAdapter of android ListView

Source: Internet
Author: User

How to Use BaseAdapter of android ListView

We usually master a fixed pattern when using a custom adapter. Make full use of convertView + cache.


private ArrayList
 
   list ;private LayoutInflater mInflater;public DetailListAdapter(Context context,ArrayList
  
    list){this.list=list;mInflater = LayoutInflater.from(context);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list == null ?0:list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if(convertView == null){convertView = mInflater.inflate(R.layout.search_info_item_layout, null);holder = new ViewHolder();holder.TimeTxt = (TextView) convertView.findViewById(R.id.time_tv);holder.AddressTxt = (TextView) convertView.findViewById(R.id._address_tv);holder.processFlagTxt = (TextView) convertView.findViewById(R.id.process_flag_tv);convertView.setTag(holder);}else{holder = (ViewHolder) convertView.getTag();}holder.TimeTxt.setText(list.get(position).getTime());holder.AddressTxt.setText(list.get(position).getAddress());holder.processFlagTxt.setText(list.get(position).getState());return convertView;}class ViewHolder{TextView TimeTxt;TextView AddressTxt;TextView processFlagTxt;}}
  
 
Recently, when downloading Baidu maps offline, I found a new method and wanted to know how Baidu android programmers wrote code. A closer look is indeed quite different.

Demo of offline download from Baidu Map

Private ArrayList
 
  
LocalMapList = null; public class LocalMapAdapter extends BaseAdapter {@ Overridepublic int getCount () {return localMapList. size () ;}@ Overridepublic Object getItem (int index) {return localMapList. get (index) ;}@ Overridepublic long getItemId (int index) {return index ;}@ Overridepublic View getView (int index, View view, ViewGroup arg2) {MKOLUpdateElement e = (MKOLUpdateElement) getItem (index); view = View. in Flate (OfflineDemo. this, R. layout. offline_localmap_list, null); initViewItem (view, e); return view;} void initViewItem (View view, final MKOLUpdateElement e) {Button display = (Button) view. findViewById (R. id. display); Button remove = (Button) view. findViewById (R. id. remove); TextView title = (TextView) view. findViewById (R. id. title); TextView update = (TextView) view. findViewById (R. id. update); TextView ratio = (TextView) view. findViewById (R. id. ratio); ratio. setText (e. ratio + "%"); title. setText (e. cityName); if (e. update) {update. setText ("updatable");} else {update. setText ("latest");} if (e. ratio! = 100) {display. setEnabled (false);} else {display. setEnabled (true);} remove. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {mOffline. remove (e. cityID); updateView () ;}}); display. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (); intent. putExtra ("x", e. geoPt. longpolling); intent. putExtra ("y", e. geoPt. latitude); intent. setClass (OfflineDemo. this, BaseMapDemo. class); startActivity (intent );}});}}
 



Related Article

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.