繁体   English   中英

如何从 ExpandableListView 中获取选定的子项?

[英]How to get selected child items from ExpandableListView?

我正在使用具有多个组项和子项的ExpandableListView 我已根据要求完成了填充数据和所有内容的工作,但我不知道如何从列表中获取选定的子项。

下面是 ExpandableList Adapter 的代码和包含可扩展列表视图的片段:

ExpandableList 适配器

public class ExpListAdapter_EnqSecond extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpListAdapter_EnqSecond(Context context, List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

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

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.sample_type_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.rowTextView);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

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

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

片段类

public class AddEnqSecond extends Fragment{

ArrayList<String> SampleList, WaterPurpostList, ConcretePurposeList, CementPurposeList, SoilPurposeList;
ExpListAdapter_EnqSecond listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
View v;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.add_enq_second, container, false);
    init();
    return v;
}

private void init() {
    SampleList = new ArrayList<>();
    SampleList = getArguments().getStringArrayList("list_sample");

    WaterPurpostList = new ArrayList<>();
    WaterPurpostList.add("Drinking Water");
    WaterPurpostList.add("Construction Water");

    ConcretePurposeList = new ArrayList<>();
    ConcretePurposeList.add("All Tests");
    ConcretePurposeList.add("Chemical Analysis");
    ConcretePurposeList.add("Cubes");
    ConcretePurposeList.add("Cylinder");

    CementPurposeList = new ArrayList<>();
    CementPurposeList.add("All Tests");
    CementPurposeList.add("Cement-Physical");
    CementPurposeList.add("Chemical Analysis");

    SoilPurposeList = new ArrayList<>();
    SoilPurposeList.add("All Tests");
    SoilPurposeList.add("Chemical Analysis");
    SoilPurposeList.add("Field Tests");
    SoilPurposeList.add("Physical Tests");

    expListView = (ExpandableListView) v.findViewById(R.id.lvExp2);
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            Log.d("AddEnqSecond", "onChildClick: " +childPosition );
            CheckBox cb = (CheckBox) v.findViewById(R.id.CheckBox01);
            if (cb != null) cb.toggle();
            Log.e("Checkbox text", cb.getText().toString());
            return false;
        }
    });
    // preparing list data
    prepareListData();

    listAdapter = new ExpListAdapter_EnqSecond(getActivity(), listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);
}

private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding Headers Data
    for (int i = 0; i < SampleList.size(); i++) {
        listDataHeader.add(SampleList.get(i).toString());
    }

    // Adding items to subElements of HeaderItems
    for (int j = 0; j < listDataHeader.size(); j++) {
        if (listDataHeader.get(j).equals("Water")) {
            listDataChild.put(listDataHeader.get(j), WaterPurpostList);
        } else if (listDataHeader.get(j).equals("Soil")) {
            listDataChild.put(listDataHeader.get(j), SoilPurposeList);
        } else if (listDataHeader.get(j).equals("Cement")) {
            listDataChild.put(listDataHeader.get(j), CementPurposeList);
        } else if (listDataHeader.get(j).equals("Concrete")) {
            listDataChild.put(listDataHeader.get(j), ConcretePurposeList);
        }
    }

}

}

请帮助我完成任务。

很可能不知道。 您需要在适配器中实现onChildClick<\/code>并自行管理选中和未选中状态。

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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