簡體   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