巧秒构造造就强大的adapter

来源:互联网 发布:优化发现环境调研报告 编辑:程序博客网 时间:2024/05/17 04:31

上一篇文章介绍了对adapter的简单认识,通过这一段时间的学习深刻认识到了adapter的强大。这篇文章将介绍一下如何巧妙地定义自己的构造函数来满足各种各样的adapter需要,同时也是对之前ListView结合Adapter逗比用例的进一步完善,希望能对大家对适配器的理解有一定的帮助!

上一个实例中将拿到的实体对象转换成一个个实体属性的数组之后才放入适配器中,总感觉这是很笨拙的方式,当时心中只有一个想法:任何一种封装都不会做成这样的,这样的局限性太强了。于是在网上找各种资料,终于解决了自己心中的疑惑,现拿改版后的代码和大家分享!

改版前:

<span style="font-size: 18px;"> </span><span style="font-size:14px;">/*子线程-解析数据*/       private  Handler handler = new Handler() {           /*声明子类中商品数量*/           public List<sonSortigns> sortingSon;          private String[] ListproName = {};            // 在Handler中获取消息,重写handleMessage()方法            @Override            public void handleMessage(Message msg) {                switch (msg.what) {                  case -1:                      Toast.makeText(SearchActivity.this, "服务器连接失败!",                              Toast.LENGTH_SHORT).show();                      break;                  case -2:                      Toast.makeText(SearchActivity.this, "哎呀,出错啦...",                              Toast.LENGTH_SHORT).show();                      break;                                case 1:                                   String temp = (String)msg.obj;                                                    //将拿到的json转换为数组                      List<productSonSorting_cate> sortingInfo = JSON.parseArray(temp,productSonSorting_cate.class);                      //小吃类商品个数                      LengthsortingSon=sortingInfo.get(0).getSonSortings().get(0).getProducts().size();                      /*循环各个商品属性加入数组*/                      for(int i=0;i<LengthsortingSon;i++){                          mTitleValues[i] =sortingInfo.get(0).getSonSortings().get(0).getProducts().get(i).getProName().toString();                          mContentValues[i] =sortingInfo.get(0).getSonSortings().get(0).getProducts().get(i).getMarkPrice().toString();                          mEvluateValues[i]=sortingInfo.get(0).getSonSortings().get(0).getProducts().get(i).getHaoping().toString();                          mSellValues[i]=sortingInfo.get(0).getSonSortings().get(0).getProducts().get(i).getAreadSell().toString();                      }                      /*适配商品属性和控件*/                      product_class_listview.setAdapter(new productsAdapter());                                        break;                  default:                      break;                }            }          }; </span>
改版后:

<span style="font-size:14px;">/*子线程-解析数据*/     private  Handler handler = new Handler() {         /*声明子类中商品数量*/         public List<sonSortigns> sortingSon;        private String[] ListproName = {};          // 在Handler中获取消息,重写handleMessage()方法          @Override          public void handleMessage(Message msg) {              switch (msg.what) {                case -1:                    Toast.makeText(SearchActivity.this, "服务器连接失败!",                            Toast.LENGTH_SHORT).show();                    break;                case -2:                    Toast.makeText(SearchActivity.this, "哎呀,出错啦...",                            Toast.LENGTH_SHORT).show();                    break;                              case 1:                                 String temp = (String)msg.obj;                                                  //将拿到的json转换为数组                    List<productSonSorting_cate> sortingInfo = JSON.parseArray(temp,productSonSorting_cate.class);                    // listview加载适配器                    product_class_listview.setAdapter(new ProductsListAdapter(SearchActivity.this,sortingInfo));                    break;                default:                    break;              }          }        };</span>

这里用到的ProductListAdapter()具体内容为:

<span style="font-size:14px;">package jczb.shoping.adapter;import java.util.List;import jczb.shoping.adapter.MainLVGrideViewAdapter.mainProList;import jczb.shoping.model.productSonSorting_cate;import jczb.shoping.model.products;import jczb.shoping.model.sonSortigns;import jczb.shoping.ui.ProductsInfoActivity;import jczb.shoping.ui.R;import jczb.shoping.ui.SearchActivity;import jczb.shoping.ui.SearchActivity.ViewHolder;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;/*适配器-适配数据&控件*/public class ProductsListAdapter extends BaseAdapter{    private Context mContext;     private List<productSonSorting_cate> mList;    public ProductsListAdapter(Context mContext,List<productSonSorting_cate> mList) {         super();         this.mContext = mContext;         this.mList = mList;         }    @Override    public int getCount() {        // TODO Auto-generated method stub        if (mList==null) {            return 0;        }else {            //返回数组长度、同时也是最终列表中item的个数            return this.mList.get(0).getSonSortings().get(0).getProducts().size();        }    }    @Override    public Object getItem(int position) {        // TODO Auto-generated method stub        if (mList == null) {             return null;         } else {             //绑定数组            return this.mList.get(position);         }     }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub        ViewHolder holder = null;         if (convertView == null) {             holder = new ViewHolder();             convertView = LayoutInflater.from(this.mContext).inflate(R.layout.activity_search_item, null,true);            //绑定控件             holder.image=(ImageView) convertView.findViewById(R.id.search_image);            holder.title=(TextView) convertView.findViewById(R.id.search_item_title);            holder.content=(TextView) convertView.findViewById(R.id.search_item_value);            holder.evaluate=(TextView) convertView.findViewById(R.id.search_item_evaluate);            holder.sell=(TextView) convertView.findViewById(R.id.search_item_sell);                     holder.search_layout=(LinearLayout) convertView.findViewById(R.id.search_layout);            //缓存列表,供下次使用            convertView.setTag(holder);         } else {             holder = (ViewHolder) convertView.getTag();         }         if (this.mList != null) {             //控件绑定数据            sonSortigns mProductsList = this.mList.get(0).getSonSortings().get(position);             holder.title.setText(mProductsList.getProducts().get(position).getProName());            holder.content.setText(mProductsList.getProducts().get(position).getShopPrice());            holder.evaluate.setText(mProductsList.getProducts().get(position).getHaoping());            holder.sell.setText(mProductsList.getProducts().get(position).getAreadSell());                  }         return convertView; }    /*定义item对象*/    public class ViewHolder {        LinearLayout search_layout;        ImageView image;        TextView title;        TextView content;        TextView evaluate;        TextView sell;    }   }</span>

    可以看出后来的用法更加方便、灵活,消除了很多的限制,我们只需要根据自己的需要将拿到的数据类型放在自己定义好的适配器中即可,当然这个适配器是根据自己的需要定义好的,在这里自己通过请求获得的是一个实体模型,所以定义时的适配器为实体数组的类型,之后在将数组的各个属性值根据需要和控件一一绑定即可!

<span style="font-size:14px;">if (this.mList != null) {             //控件绑定数据            sonSortigns mProductsList = this.mList.get(0).getSonSortings().get(position);             holder.title.setText(mProductsList.getProducts().get(position).getProName());            holder.content.setText(mProductsList.getProducts().get(position).getShopPrice());            holder.evaluate.setText(mProductsList.getProducts().get(position).getHaoping());            holder.sell.setText(mProductsList.getProducts().get(position).getAreadSell());              }   </span>

其他的按照统一格式去写即可,唯一需要做的就是将拿到的数据类型解析成自己需要的数据通过viewholer赋值给对应的控件即可!对适配器研究了大概有一周了,期间经历了很多的问题,也被一个个渺小的问题绊倒过很多次,现在走过来确实感受到了适配器的强大,也体会到了大牛们服务于人的崇高境界!

0 0
原创粉丝点击