繁体   English   中英

带分隔线的自定义ListView适配器

[英]Custom ListView Adapter with divider

我想制作一个自定义列表视图适配器。 这是我的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个组,我想列出每个组的子级。 另外,对于每个孩子,我需要显示一种分隔符来分隔每个组。

这是我的自定义listview适配器:

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;
}

子模型

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

编辑:同样,当我将组列表传递给适配器时,仅适用于第一个组的子级。

编辑2:现在在我的组适配器中,我添加了一个列表视图:

@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;
}
}

感谢您的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM