Android ListView分组

我们常常需要在列表(ListView)中分组填充和展示数据正如以下的效果,实现效果如下:

通过将添加到ListView适配器的ArrayList内数据进行分组,然后通过重写simpleAdatper,切换listview的item。达到分组的效果。

Activity的XML文件中listview

  <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip" >
    </ListView>

正常内容的listview中item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" > 
     <TextView 
        android:id="@+id/item_title"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textSize="@dimen/text_content_size"
        android:textColor="@color/color_font_wb_listview_title"
        />  
       <TextView 
        android:id="@+id/item_statts"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:gravity="right"
        android:layout_gravity="end"
        android:layout_width ="wrap_content"
        android:textSize="@dimen/text_content_size"
        android:textColor="@color/color_font_wb_listview_status_normol"
        />
</LinearLayout>


充当分组空挡的item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="25dip"
    android:background="#f6f6f6"
    android:orientation="vertical" > 
     <TextView 
        android:id="@+id/item_group_"
        android:layout_height="10dip"
        android:layout_width="match_parent"
        android:textSize="@dimen/text_content_size"
        android:text="  "
        />  
</LinearLayout>

首先是获取ArrayList()以下是通过解析网络获取的json,分别获取3中状态的信息,然后分别存入3个List中,最终合并到一个list,中间插入内容为“分组”的元素。

<span style="font-size:18px;">List<HashMap<String, Object>> data_list_map = new ArrayList<HashMap<String, Object>>();
List<HashMap<String, Object>> data_list_map_handle = new ArrayList<HashMap<String, Object>>();
List<HashMap<String, Object>> data_list_map_pause = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> orderMap;
JSONObject order_list_object;
try {
order_list_object = new JSONObject(jsonString);
JSONObject order_list_data_object = order_list_object
.getJSONObject("data");
JSONArray order_list_content_array = order_list_data_object
.getJSONArray("content");
// 解析json 数组
if (order_list_content_array != null) {
for (int i = 0; i < order_list_content_array.length(); i++) {
JSONObject object = order_list_content_array
.getJSONObject(i);
orderMap = new HashMap<String, Object>();
if (object.has("elevator")) {
orderMap.put("elevator", object.getString("elevator"));
}
if (object.has("completetime")) {
orderMap.put("completetime",
object.getString("completetime"));
} 
if (object.has("statusName")) {
if(object.getString("statusName").equals("待接收")){
orderMap.put("statusName",
"待接受");
}
else{orderMap.put("statusName",
object.getString("statusName"));}
}
if("待接受".equals(orderMap.get("statusName")))
{data_list_map.add(orderMap);}
if("处理中".equals(orderMap.get("statusName")))
{data_list_map_handle.add(orderMap);}
if("已暂停".equals(orderMap.get("statusName")))
{data_list_map_pause.add(orderMap);}
}
//3个List合并
orderMap = new HashMap<String, Object>();
orderMap.put("statusName", "分组");
data_list_map.add(orderMap);
data_list_map.addAll(data_list_map_handle);
data_list_map.add(orderMap);
data_list_map.addAll(data_list_map_pause);</span>


重写的simpleAdapter 根据传入的 list中元素的值是否为“分组”,不是则选择采用正常的item,是则采用充当分割栏的item.

public class OrderListAdapter extends SimpleAdapter {
Context context;
ArrayList<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();
String[] str;
int[] viewId;
int layoutId;
class ViewHolder {
TextView text_title, text_status;
}
public OrderListAdapter(Context context,
ArrayList<HashMap<String, Object>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
this.dataList = data;
this.context = context;
this.str = from;
this.viewId = to;
this.layoutId = resource;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// 页面
ViewHolder holder;
LayoutInflater inflater = LayoutInflater.from(context);
if ("分组".equals(dataList.get(position).get("statusName"))) {
convertView = inflater.inflate(R.layout.wb_listview_group_item,
null);
} else {
convertView = inflater.inflate(layoutId, null);
holder = new ViewHolder();
holder.text_title = (TextView) convertView.findViewById(viewId[0]);
holder.text_title.setText((CharSequence) dataList.get(position)
.get(str[0]));
holder.text_status = (TextView) convertView.findViewById(viewId[1]);
holder.text_status.setText((CharSequence) dataList.get(position)
.get(str[1]));
}
}
return convertView;
}
}


Activity中的调用

Simpledapter simpledapter = new OrderListAdapter(this, (ArrayList<HashMap<String, Object>>) datalist,
R.layout.wb_listview_item,
new String[] { "elevator", "statusName" }, new int[] {
R.id.item_title, R.id.item_statts });
listview.setAdapter(simpledapter);



当然实现分组数据展示还有其他方法,仅供参考,继续学习。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值