如何获得Group& ExpandableListView上的子项使用getter& setter方法

时间:2016-09-07 06:14:05

标签: android expandablelistview

嘿伙计们我想在ExpandableListView的{​​{1}}&{ setOnGroupClickListener但没有得到它的项目。

这是我的代码:

Group.java

setOnChildClickListener

Child.java

public class Group {

private String Name;
private ArrayList<Child> Items;

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public ArrayList<Child> getItems() {
    return Items;
}

public void setItems(ArrayList<Child> items) {
    Items = items;
}
}

ExpandListAdapter.java

public class Child {
private String Name;

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}
}

MainActivity.java

public class ExpandListAdapter extends BaseExpandableListAdapter {

private Context context;
private ArrayList<Group> groups;

public ExpandListAdapter(Context context, ArrayList<Group> groups) {
    this.context = context;
    this.groups = groups;
}

@Override
public int getGroupCount() {
    return groups.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<Child> chList = groups.get(groupPosition).getItems();
    return chList.get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    Group group = (Group) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.group_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.group_name);
    tv.setText(group.getName());
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    Child child = (Child) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_item, null);
    }

    TextView tv = (TextView) convertView.findViewById(R.id.country_name);

    tv.setText(child.getName().toString());

    return convertView;
}

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

1 个答案:

答案 0 :(得分:1)

将您的商品作为update Item where PubYear < 2000 set LostFee = (LostFee)*1.10; getChildView中的代码添加到视图中,如下所示:

getGroupView

然后您可以在回调中获取项目,如

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    Child child = (Child) getChild(groupPosition, childPosition);
    // keep your code
    // set item as a tag
    convertView.setTag(child);
    return convertView;
}