2014-12-22 93 views
0

我一直在關注這個邏輯: A multiple choice list view using expansible list獲取所選清單項目擴大的ListView的Android

和管理的expandinglistview創建一組複選框。

所以它看起來是這樣的:

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)。我只是希望能夠從檢查列表中讀取所有值,但沒有找到任何可以幫助的值。

我的適配器是這個例子所採取的適配器(太長的方式添加在這裏)

請幫幫忙!

+0

我假定塊是在某處CountryAdapter放在哪裏?你能告訴你如何聲明'checked'變量和'selectedItems'變量嗎?我只在該適配器中看到一個'checkedPositions'變量。 –

回答