获取选定的核对表项目,扩展ListView Android

时间:2014-12-22 20:55:06

标签: android listview expandablelistview

我一直遵循这个逻辑: A multiple choice list view using expansible list

并设法在expandedlistview中创建一组复选框。

所以它看起来像:

    catList = new ArrayList<GroupHead>();
    countries = new ArrayList<Country>();
    //Read the XML string
    //Break down the XML into parts

    try 
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builders = factory.newDocumentBuilder();
        Document document = builders.parse(new InputSource(new StringReader(XMLResult)));  

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        document.getDocumentElement().normalize();

        String rootNode = document.getDocumentElement().getNodeName().toString();

        if (rootNode.equals("Results"))
        {
            NodeList nList = document.getElementsByTagName("Result");

            System.out.println("----------------------------");

            GroupHead cat1 = null;
            List<GroupSession> result = new ArrayList<GroupSession>();

            for (int temp = 0; temp < nList.getLength(); temp++) 
            {

                NodeList TaskTopic = null;
                Element day = (Element) nList.item(temp);
                String Days = day.getAttribute("Day");

                //Add one header (being the PracticalTopic)
                cat1 = createCategory(Days);

                TaskTopic = day.getElementsByTagName("Session");
                for (int j = 0; j < TaskTopic.getLength(); ++j)
                {
                    Element option = (Element) TaskTopic.item(j);
                    String optionText = option.getFirstChild().getNodeValue();
                    //Add multiple items and set them to that list 

                    String[] results = optionText.split(", ");
                    String StartTimes = results[0];
                    String Lab = results[1];

                    sessions = new ArrayList<String>
                    (Arrays.asList(optionText));

                    ArrayList<String> citiesAustralia = new ArrayList<String>(
                            Arrays.asList(optionText));

                        countries.add(new Country(Days, citiesAustralia));


                    GroupSession item = new GroupSession(StartTimes, Lab);
                    result.add(item);
                    cat1.setItemList(result);
                }
              //Add them to the arrayList
                catList.add(cat1);
            }
        }
    }
    catch (Exception e)
    {
        e.getMessage();
    }
}
else
{
    Toast.makeText(MainActivity.this, "There are no group sessions available for you", Toast.LENGTH_LONG).show(); 
}

adapter = new CountryAdapter(this, countries);

expListView.setAdapter(adapter);

// The choice mode has been moved from list view to adapter in order
// to not extend the class ExpansibleListView
adapter.setChoiceMode(CountryAdapter.CHOICE_MODE_MULTIPLE);

// Handle the click when the user clicks an any child
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() 
{

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) 
    {
        adapter.setClicked(groupPosition, childPosition);
        return false;
    }
});

现在我想做的是点击一个按钮并返回所有选定的项目值。我有这样的事情:

for (int i = 0; i < checked.size(); i++) 
{
    int position = checked.keyAt(i);
    if (checked.valueAt(i) != null)
        selectedItems.add(checked.valueAt(i));
}

但它不允许我添加checked.valueAt(i)。我只是希望能够从已检查的列表中读取所有值,但找不到任何可以帮助的值。

我的适配器是从示例中获取的适配器(这里添加的时间太长了)

请帮忙!

1 个答案:

答案 0 :(得分:1)

int key = 0;
for(int i = 0; i < checked.size(); i++) {
   key = checked.keyAt(i);
   selectedItems.add(checked.valueAt(key));
}

来源:https://stackoverflow.com/a/8006994/4193263