简体   繁体   中英

Custom ListView Adapter with divider

I wanna make a custom list view adapter. This is my 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
}

The problem is that the json have 2 group, and I wanna list the child of each group. Also, for each child, I need to show a kind of divider to separate each group.

This is my custom listview adapter:

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

EDIT: Also when I pass the list of group to the adapter, only works with the child of the first group.

EDIT 2: Now in my group adapter I added a 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;
}

EDIT 3: Adding 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;
}
}

Thanks for helping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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