Android ListView分组显示

ListView的实现方法也是普通的实现方法。只不过在list列表中加入groupkey信息。在渲染的时候要判断是否是分组的标题。

就是在使用不同的两个View的时候存在这种情况,convertView不为空但是由于它渲染到别的layout的关系。它里面内容的view是为空的经常会出现空指针的错误。

我这里使用了一个非常低效的方法,下次应该会改进一下。

 

先贴上自己实现的adapter

  1 import java.util.ArrayList;
  2 import java.util.HashMap;
  3 import java.util.Iterator;
  4 import java.util.List;
  5 import java.util.Map;
  6 import java.util.Set;
  7 
  8 import android.graphics.Bitmap;
  9 import android.os.AsyncTask;
 10 import android.view.View;
 11 import android.view.ViewGroup;
 12 import android.widget.BaseAdapter;
 13 import android.widget.ImageView;
 14 import android.widget.TextView;
 15 import com.enick.project.base.BaseActivity;
 16 import com.enick.project.doman.OrderAll;
 17 import com.enick.project.doman.OrderAll.list;
 18 import com.enick.project.ui.CH_RackingListDetailActivity;
 19 import com.enick.project.utils.ViewUtil;
 20 import com.enick.zdproject.R;
 21 
 22 public class ClothesGroupAdapter extends BaseAdapter {
 23 
 24     private Map<String, List<OrderAll.list>> map = new HashMap<String, List<list>>();
 25     private List<OrderAll.list> list = new ArrayList<OrderAll.list>();
 26     private List<OrderAll.list> temp = null;
 27     private final static String TAG = "RZ";
 28 
 29     public ClothesGroupAdapter(List<OrderAll.list> temp) {
 30         this.temp = temp; 
 31     }
 32     
 33     @Override
 34     public void notifyDataSetChanged() {
 35         map.clear();
 36         list.clear();
 37         //先把数据装到map里
 38         for (int i = 0; i < temp.size(); i++) {
 39             if(temp.get(i) instanceof list){            
 40                 List<list> tempList =  map.get(temp.get(i).getItemname());
 41                 if(tempList == null){
 42                     tempList = new ArrayList<OrderAll.list>();
 43                     tempList.add(temp.get(i));
 44                     map.put(temp.get(i).getItemname(),tempList);    
 45                 }else{
 46                     tempList.add(temp.get(i));
 47                 }
 48             }
 49 
 50         }
 51         //依据map的特性,键就是groupkey。值就是list
 52         Set<Map.Entry<String, List<OrderAll.list>>> set = map.entrySet();
 53         for (Iterator<Map.Entry<String, List<OrderAll.list>>> it = set
 54                 .iterator(); it.hasNext();) {
 55             Map.Entry<String, List<OrderAll.list>> entry = (Map.Entry<String, List<OrderAll.list>>) it
 56                     .next();
 57             OrderAll order = new OrderAll();
 58             OrderAll.list root = order.getNewList();
 59             root.setItemname(entry.getKey());
 60             root.setGroupkey(true);
 61             list.add(root);
 62             for (int i = 0; i < entry.getValue().size(); i++) {
 63                 entry.getValue().get(i).setGroupkey(false);
 64                 list.add(entry.getValue().get(i));
 65             }
 66         }
 67         super.notifyDataSetChanged();
 68     }
 69 
 70     @Override
 71     public int getCount() {
 72         return list.size();
 73     }
 74 
 75     @Override
 76     public Object getItem(int pos) {
 77         return list.get(pos);
 78     }
 79 
 80     @Override
 81     public long getItemId(int pos) {
 82         return pos;
 83     }
 84 
 85     @Override
 86     public View getView(final int position, View convertView, ViewGroup parent) {
 87         ClothesHolder clothesHolder = null;
 88         //非常低性能的做法,每次都重新初始化
 89         if (list.get(position).isGroupkey()) {        
 90             convertView = ViewUtil.buildView(R.layout.ch_racking_list_tag_row);
 91             clothesHolder = new ClothesHolder();
 92             clothesHolder.tvTagName = (TextView) convertView.findViewById(R.id.tvTagName);
 93             String itemName = list.get(position).getItemname();
 94             if(!ViewUtil.isStrEmpty(itemName)){
 95                 clothesHolder.tvTagName.setText(itemName);
 96             }
 97         }else{
 98             convertView = ViewUtil.buildView(R.layout.ch_racking_list_row);
 99             clothesHolder = new ClothesHolder();
100             clothesHolder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
101             clothesHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
102             clothesHolder.tvWenti = (TextView) convertView.findViewById(R.id.tvWenti);
103             clothesHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
104             String subitemName = list.get(position).getSubitemname();
105             if (!ViewUtil.isStrEmpty(subitemName)) {
106                 clothesHolder.tvName.setText(subitemName);
107             }
108 
109             String price = list.get(position).getPrice();
110             if (!ViewUtil.isStrEmpty(price)) {
111                 clothesHolder.tvPrice.setText(price);
112             }
113 
114             String wenti = list.get(position).getWenti();
115             if (!ViewUtil.isStrEmpty(wenti)) {
116                 clothesHolder.tvWenti.setText("问题!");
117             }
118             //网络加载图片,从别的服务器加载
119             final String  url = list.get(position).getUrl();
120             if (!ViewUtil.isStrEmpty(url)) {
121                 if(ViewUtil.isuploadedphoto(url)){//网络图片
122                     final ImageView temp=clothesHolder.ivImage;
123                     new AsyncTask<Void, Void, Bitmap>() {
124                         protected Bitmap doInBackground(Void... p) {
125                             return ViewUtil.getHttpBitmap(url,60,60);
126                         }
127                         @Override
128                         public void onPostExecute(Bitmap result) {
129                             super.onPostExecute(result);
130                             temp.setImageBitmap(result);
131                         }
132 
133                     }.execute();
134                 }else{
135                     clothesHolder.ivImage.setImageBitmap(ViewUtil.extractMiniThumb(
136                             ViewUtil.getFilePathBitmap(url), 60, 60));
137                 }            
138                 clothesHolder.ivImage.setImageBitmap(ViewUtil.extractMiniThumb(ViewUtil
139                         .getFilePathBitmap(url), 60, 60));
140             }
141             //添加事件
142             convertView.setOnClickListener(new View.OnClickListener() {
143                 @Override
144                 public void onClick(View v) {
145                     BaseActivity.currentActivity.startCOActivity(CH_RackingListDetailActivity.class,
146                                     OrderAll.CLOTHES_ID,list.get(position).getId());
147                 }
148             });
149         }
150         return convertView;
151     }
152 
153     private class ClothesHolder {
154         ImageView ivImage;
155         TextView tvName;
156         TextView tvTagName;
157         TextView tvWenti;
158         TextView tvPrice;
159         
160     }
161 
162 }

 

acitivity只要传入数据集合就可以了

1 private List<OrderAll.list> listclothes = new ArrayList<OrderAll.list>();
2 private ListView lvClothes;
3 private ClothesGroupAdapter adapter;
4 adapter = new ClothesGroupAdapter(listclothes);
5 lvClothes.setAdapter(adapter);
6 
7 listclothes.clear();
8 listclothes.addAll(OrderData.getTempOrderAll().getList());
9 adapter.notifyDataSetChanged();

 

界面效果图

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。