Android listview sectionHeader:在单个TextView中显示相关类别的项目

时间:2016-03-16 08:10:07

标签: android listview

我正在开发一个应用程序,我将获得如下所示的JSON响应,用于将其类别显示在Listview中的服务,其中类别为标题,服务为行项目。目前它的工作正常,但现在需要显示逗号分隔的行项目,而不是单独的行/单独项目。

JSON

[
{
categoryId: 1
categoryName: "Automobile"
serviceName: "Buy Sell"
serviceId: 3
}

{
categoryId: 1
categoryName: "Automobile"
serviceName: "Wash"
serviceId: 5
}

{
categoryId: 1
categoryName: "Automobile"
serviceName: "Spare Parts"
serviceId: 2
}

{
categoryId: 3
categoryName: "Education"
serviceName: "Teaching"
serviceId: 1
}

{
categoryId: 7
categoryName: "Food"
serviceName: "Restaurant"
serviceId: 5
}
]

现在我想要的是显示这些

enter image description here

这是单个项目/ Textview中以逗号分隔的服务。目前我正在使用单个TextView作为行项目。以下是我的自定义适配器代码

class CustomAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    LinearLayout linearLayout;

    private ArrayList<String> mData = new ArrayList<String>();
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
    private LayoutInflater mInflater;

    public CustomAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(final String item) {
        mData.add(item);
        notifyDataSetChanged();
    }

    public void addSectionHeaderItem(final String item) {
        mData.add(item);
        sectionHeader.add(mData.size() - 1);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
    }

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

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public String getItem(int position) {
        return mData.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        int rowType = getItemViewType(position);

        if (convertView == null) {
            holder = new ViewHolder();
            switch (rowType) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.snippet_item1, null);
                    linearLayout = (LinearLayout) convertView.findViewById(R.id.parent);
                    holder.textView = (TextView) convertView.findViewById(R.id.text);
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.snippet_item2, null);
                    holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
                    break;
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        switch (rowType) {
            case TYPE_ITEM:
                holder.textView.setText(mData.get(position));
                break;
            case TYPE_SEPARATOR:
                holder.textView.setText(mData.get(position));
                break;
        }

        return convertView;
    }

    public static class ViewHolder {
        public TextView textView;
    }
}

由于getView()方法的行为,我真的陷入其中,请告诉我如何通过使用上面的代码实现这一点,任何帮助都将受到高度赞赏,提前感谢。

修改

这就是我加载JSON的方式

first_loop = 1;
 try {
                for (int i = 0; i < ja.length(); i++) {
                    checkLoop++;
                    obj = ja.getJSONObject(i);
                    if (first_loop == 1) {
                        headerList.add("head");
                        currentCategory = obj.getString("categoryId");
                        adpter.addSectionHeaderItem(obj.getString("categoryName"));

                        adpter.addItem(obj.getString("serviceName"));
                        headerList.add("detail");

                        first_loop++;
                    } else {
                        if (currentCategory.equals(obj.getString("categoryId"))) {
                            currentCategory = obj.getString("categoryId");
                            adpter.addItem(obj.getString("serviceName"));
                            headerList.add("detail");
                        } else {
                            headerList.add("head");
                            currentCategory = obj.getString("categoryId");
                            adpter.addSectionHeaderItem(obj.getString("categoryName"));
                            adpter.addItem(obj.getString("serviceName"));
                            headerList.add("detail");
                        }
                    }
                }

                if (checkLoop <= 0){
                    headeradpter.addSectionHeaderItem("");
                    headeradpter.addItem("N/A");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

0 个答案:

没有答案