具有分隔符

时间:2016-09-01 12:39:08

标签: android json android-adapter custom-lists

我想制作一个自定义列表视图适配器。这是我的json:

{
    "Model": [{
        "Group": "Group 1",
        "person": [{
            "Name": "Name 1",
            "Country": "Country 1"
        }]
    }, {
        "Group": "Group 2",
        "person": [{
            "Name": "Name 1",
            "Country": "Country 1"
        }, {
            "Name": "Name 2",
            "Country": "Country 2"
        }]
    }],
    "Success": true
}

问题是json有2组,我想列出每组的孩子。此外,对于每个孩子,我需要展示一种分隔符来分隔每个组。

这是我的自定义列表视图适配器:

public class GroupListAdapter extends BaseAdapter {

Context context;
ArrayList<GroupModel> groupList;

public GroupListAdapter(Context context, ArrayList<GroupModel> groupList){
    this.context               = context;
    this.groupList = groupList;
}

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

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

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

static class ViewHolder{
    private TextView txtName;
    private TextView txtCountry;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.fila_contacto, null);
        viewHolder = new ViewHolder();

        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
        viewHolder.txtCountry = (TextView) convertView.findViewById(R.id.txtCountry);

        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}
}

GroupList.java

public class GroupModel implements Serializable {
    public String DescriptionDivider;
    public ArrayList<ChildModel> ChildModel;
}

ChildModel

public class ChildModel implements Serializable {
public String Name;
public String Country;
}

修改 此外,当我将组列表传递给适配器时,仅适用于第一组的子项。

编辑2: 现在,在我的组适配器中,我添加了一个listview:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.fila_contacto, null);
        viewHolder = new ViewHolder();
        GroupList groupList = groupList.get(position);

        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
        viewHolder.lstChild = (ListView) convertView.findViewById(R.id.lstChild);

        listViewChildAdapter childAdapter= new listViewChildAdapter (this.context, groupList.ChildModel);
        lstChild.setAdapter(childAdapter);

        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

编辑3:添加ListViewChildAdapter

public class listViewChildAdapter  extends BaseAdapter {

Context context;
ArrayList<ChildModel> childList;

public listViewChildAdapter(Context context, ArrayList<ChildModel> childList){
    this.context = context
    this.childList = childList;
}

static class ViewHolder{
    private TextView txtName;
    private TextView txtCountry;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.child_row, parent, true);
        viewHolder = new ViewHolder();

        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
        viewHolder.txtCountry = (TextView) convertView.findViewById(R.id.txtCountry);


        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    ChildModel child = childList.get(position);

    // region Campos NO NULLEABLES
    viewHolder.txtName.setText(child.Name);
    viewHolder.txtCountry.setText(child.Country);
    // endregion

    return convertView;
}
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

相关问题