0

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

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54
  • you can try this if(jsonObject.has("Group 1")) – h_patel Sep 01 '16 at 12:42
  • @Himani Where I have to put this? Also When I pass to the adapter the list of group, the adapter only works with the child for the first group – Faustino Gagneten Sep 01 '16 at 12:44
  • hey you can put all the child object in single list while parsing and simply show them using `ListView`. Wouldn't that solve your problem? – himanshu1496 Sep 01 '16 at 12:46
  • can put this condition in JSON parsing . and if condition true show them in listview . – h_patel Sep 01 '16 at 12:48
  • @himanshu1496 The problem that I have to display a divider for each group. Not only show the childs – Faustino Gagneten Sep 01 '16 at 12:49
  • 1
    That is something which can be handled while parsing, have a `flag` say `endOfGroup`, and mark it `true` for every last child of that group, inside the `listView` show the divider view if that flag is true otherwise don't. – himanshu1496 Sep 01 '16 at 12:52
  • Hey, create a arraylist of GroupModel type. And pass it to your adapter. It will create a two item row as per given json above. Now into the adapter you have to check the size of **GroupModel** arraylist. If it is more then zero, then you can inflate custom layout into a child parent. – Rahul Sharma Sep 01 '16 at 12:53
  • First you need to have the arraylist of items inside adapter and set it throught the position on getView() into your viewholder views. Then for the divider of the list you need to set the style for it, check : http://stackoverflow.com/questions/3979218/android-listview-divider – AmirG Sep 01 '16 at 12:59
  • @AmirG Hey, Now I have the listview of child into the getview group and I made another adapter for the child. Into getview() of the group adapter I set a listview and put another adapter, but only show me 1 item not more than 1 – Faustino Gagneten Sep 01 '16 at 14:01
  • @FederickJons can you show the code? – AmirG Sep 01 '16 at 14:23
  • @AmirG Please, see now EDIT 2 – Faustino Gagneten Sep 01 '16 at 14:36
  • @FederickJons add also the child adapter please – AmirG Sep 01 '16 at 14:40
  • @AmirG now see new changes. – Faustino Gagneten Sep 01 '16 at 14:50
  • @FederickJons ok first the child adapter is not extending from base adapter, second when you do this you need to override the same methods that you override for adapter of parent – AmirG Sep 01 '16 at 14:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122432/discussion-between-federick-jons-and-amirg). – Faustino Gagneten Sep 01 '16 at 14:57

1 Answers1

0

Have you tried ExpandableListView https://developer.android.com/reference/android/widget/ExpandableListView.html ?