包含Parent和Child的适配器的ExpandableListView

时间:2016-09-09 11:18:47

标签: java android expandablelistview

我尝试使用ExpandableListView一些Detials,例如跟踪号码和ParentHeader中的位置以及孩子的客户名称,地址,联系号码,但在编译我的代码时它没有显示任何内容,查看我的代码并帮助我在代码中缺少的内容。我在下面试过的代码

MainActivity:

public class MainActivity extends AppCompatActivity {

ParentAdapter listAdapter;
ExpandableListView expandableListView;
ArrayList<ListHeader> listDataHeader;
ArrayList<ListChild> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    expandableListView = (ExpandableListView)findViewById(R.id.exp_lst_view);
    listAdapter = new ParentAdapter(this,listDataHeader,listDataChild);
    ListData();
    expandableListView.setAdapter(listAdapter);

}

private void ListData() {

    listDataHeader = new ArrayList<ListHeader>();
    listDataChild = new ArrayList<ListChild>();

    ListHeader listHeader = new ListHeader();
    listHeader.setLocation("CHENNAI");
    listHeader.setTrackingNumber("ID5239");
    listDataHeader.add(listHeader);

    ListChild listChild = new ListChild();
    listChild.setCustomer("CUSTOMER 1");
    listChild.setAddress("28/34 xxx APTT, ccc rd , dddd-0000009");
    listChild.setContactNumber("0987654321");
    listDataChild.add(listChild);
}}

适配器:

public class ParentAdapter extends BaseExpandableListAdapter{

private Context context;
private ArrayList<ListHeader> listDataHeader;
private ArrayList<ListChild> listDataChild;
private LayoutInflater mInflater;

public ParentAdapter(Context context, ArrayList<ListHeader> listDataHeader, ArrayList<ListChild> listDataChild) {
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listDataChild = listDataChild;
}


@Override
public int getGroupCount() {
    return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
    return 0;
}

@Override
public Object getGroup(int groupPosition) {
    return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return null;
}

@Override
public long getGroupId(int groupPosition) {
    return 0;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    View v = convertView;
    ParentViewHolder viewHolder;
    if (v == null) {
        viewHolder = new ParentViewHolder();
        v = mInflater.inflate(R.layout.adapter_header, null);
        viewHolder.txtTrackingNumber = (TextView) v.findViewById(R.id.txt_tracking_number);
        viewHolder.txtLocation = (TextView) v.findViewById(R.id.txt_location);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ParentViewHolder) v.getTag();
    }

    return v;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    View v = convertView;
    ChildViewHolder viewHolder;
    if (v == null) {
        viewHolder = new ChildViewHolder();
        v = mInflater.inflate(R.layout.adapter_header, null);
        viewHolder.txtCustomer = (TextView) v.findViewById(R.id.txt_customer);
        viewHolder.txtAddress = (TextView) v.findViewById(R.id.txt_address);
        viewHolder.txtContactNumber = (TextView) v.findViewById(R.id.txt_contact_number);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ChildViewHolder) v.getTag();
    }

    return v;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public class ParentViewHolder {
    private TextView txtTrackingNumber;
    private TextView txtLocation;
}

public class ChildViewHolder {
    private TextView txtCustomer;
    private TextView txtAddress;
    private TextView txtContactNumber;
}}

0 个答案:

没有答案