listview中每个json数据的第一项只有不同的布局android

时间:2015-11-04 12:05:08

标签: android listview adapter

我正在处理自定义listview,其标题为StoreName,孩子为items of its store

我的json数据:

{
    "status": "success",
    "response": [
        {
            "store_id": "1",
            "store_name": "Prashant Stores",
            "items": [
                {
                    "item_id": "1",
                    "item_name": "Kellogs Cornflakes",
                    "item_price": "75",
                    "item_size": "500 mg",
                    "item_quantity": "1",
                    "item_total": "75",
                    "delivery_boy_status_id": "3",
                    "delivery_boy_status": "Delivered",
                    "is_available": "1"
                },
                {
                    "item_id": "2",
                    "item_name": "Chocos",
                    "item_price": "75",
                    "item_size": "1 kg",
                    "item_quantity": "1",
                    "item_total": "75",
                    "delivery_boy_status_id": "3",
                    "delivery_boy_status": "Delivered",
                    "is_available": "1"
                }
            ]
        },
        {
            "store_id": "5",
            "store_name": "Corner Stores",
            "items": [
                {
                    "item_id": "4",
                    "item_name": "Chocos",
                    "item_price": "400",
                    "item_size": "1 kg",
                    "item_quantity": "1",
                    "item_total": "400",
                    "delivery_boy_status_id": "1",
                    "delivery_boy_status": "To be Collected",
                    "is_available": "1"
                }
            ]
        }
    ],
    "message": ""
}

我希望listview像这样

enter image description here

我的适配器

public class CustomerwiseDeatilsAdapter extends BaseAdapter {


    UtilClass utilClass;
    private Activity context;
    private List<CustomerwiseDetailsModel> list;
    private String TAG = "OrderHistoryAdapter";
    int isHeaderAdded = 0;
    List<String> storeList;

    public CustomerwiseDeatilsAdapter(Activity context, List<CustomerwiseDetailsModel> list) {
        this.context = context;
        this.list = list;
        storeList = new ArrayList<>();
        utilClass = new UtilClass();
    }


    @Override
    public int getCount() {

        return list.size();
    }


    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        /*if (position == 0) {
            return 0;
        } else {
            return 1;
        }*/
        return position%2;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        final CustomerwiseDetailsModel model = list.get(position);


        if (getItemViewType(position) == 0) {
            ViewHolderHeader viewHolderHeader;
            if (convertView == null) {
                viewHolderHeader = new ViewHolderHeader();
                convertView = inflater.inflate(R.layout.listview_row_neworders_customer_wise_details, parent, false);
                viewHolderHeader.txt_store_name = (TextView) convertView.findViewById(R.id.storename);
                //for each field of holder find the subview
                convertView.setTag(viewHolderHeader);
            } else {
                viewHolderHeader = (ViewHolderHeader) convertView.getTag();
            }
            //set the data in subview with holder fields
            viewHolderHeader.txt_store_name.setText(model.getStore_name());
        } else {
            ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = inflater.inflate(R.layout.neworders_customer_wise_details_linearlayout, null, false);

                viewHolder.txt_item_name = (TextView) convertView.findViewById(R.id.txt_product_name);
                viewHolder.txt_item_size = (TextView) convertView.findViewById(R.id.txt_product_weight);
                viewHolder.txt_item_quantity = (TextView) convertView.findViewById(R.id.txt_recurring_total_price_qnty);
                viewHolder.txt_item_total = (TextView) convertView.findViewById(R.id.txt_recurring_total_price);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.txt_item_name.setText(model.getItem_name());
            viewHolder.txt_item_size.setText(model.getItem_size());
            viewHolder.txt_item_quantity.setText(model.getItem_price() + " x" + model.getItem_quantity());
            viewHolder.txt_item_total.setText(model.getItem_total());
        }





        return convertView;
    }

    class ViewHolder {
        TextView txt_item_name;
        TextView txt_item_size;
        TextView txt_item_quantity;
        TextView txt_item_total;
    }

    class ViewHolderHeader {
        TextView txt_store_name;
    }
}

我的输出: enter image description here

1 个答案:

答案 0 :(得分:1)

一个想法。对所有单元格使用相同的布局。在单元格布局中添加类别的标题。然后在getView上决定是否要显示标题或使用Visibility.GONE隐藏它。要确定这一点,你可以看到数组中的当前和下一个元素,以检查类别是不同(显示)还是相同(隐藏)