2011-09-27 23 views
0

這裏有兩個問題。遵循一個教程,我需要一點幫助,以適應我的項目。 1)每個小組的孩子都一樣。例如,Arraylist餐廳包含「Dining Commons」組的兒童,我需要製作兩個包含學術建築和住宅建築的數據列表,並且需要成爲他們各自父母的孩子。擴展名單中的所有家長都有同樣的子女

2)是否有可能使孩子們可點擊的項目?使用clicklistener或類似的東西。

java源碼。

package com.bogotobogo.android.smplexpandable; 

import android.app.ExpandableListActivity; 
import android.os.Bundle; 
import android.widget.ExpandableListAdapter; 
import android.widget.SimpleExpandableListAdapter; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

/** 
* Demonstrates expandable lists backed by a Simple Map-based adapter 
*/ 
public class SmplExpandable extends ExpandableListActivity { 
    private static final String NAME = "NAME"; 
    private static final String IS_EVEN = "IS_EVEN"; 

    ArrayList buildings = BuildingList(); 
    ArrayList diningCommonBuildings = DiningList(); 

    private ExpandableListAdapter mAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); 
     List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); 
     for (int i = 0; i < buildings.size(); i++) { 
      Map<String, String> curGroupMap = new HashMap<String, String>(); 
      groupData.add(curGroupMap); 
      curGroupMap.put(NAME, (String) buildings.get(i)); 
      //curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd"); 

      List<Map<String, String>> dining = new ArrayList<Map<String, String>>(); 

      for (int j = 0; j < diningCommonBuildings.size(); j++) { 
       Map<String, String> curChildMap = new HashMap<String, String>(); 
       dining.add(curChildMap); 
       curChildMap.put(NAME, (String) diningCommonBuildings.get(j)); 
       //curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd"); 
      } 
      childData.add(dining); 
     } 

     // Set up our adapter 
     mAdapter = new SimpleExpandableListAdapter(
       this, 
       groupData, 
       android.R.layout.simple_expandable_list_item_1, 
       new String[] { NAME, IS_EVEN }, 
       new int[] { android.R.id.text1, android.R.id.text2 }, 
       childData, 
       android.R.layout.simple_expandable_list_item_2, 
       new String[] { NAME, IS_EVEN }, 
       new int[] { android.R.id.text1, android.R.id.text2 } 
       ); 
     setListAdapter(mAdapter); 
    } 

    private ArrayList BuildingList() { 

       ArrayList buildings = new ArrayList(); 
       buildings.add("Academic Buildings"); 
       buildings.add("Dining Commons"); 
       buildings.add("Residential Buildings"); 
       return buildings; 
      } 
    private ArrayList DiningList() { 

     ArrayList dining = new ArrayList(); 
     dining.add("Berkshire"); 
     dining.add("Franklin"); 
     dining.add("Hampden"); 
     dining.add("Hampshire"); 
     dining.add("Worcester"); 
     return dining; 
    } 

} 
+0

這應該是兩個獨立的問題,因爲你有兩個不同的問題。如果一個人只能回答你的問題的一部分,那麼下一個人不能因爲回答其他部分而得分。 –

+1

抱歉。如果我有多個部分的問題,生病會做出不同的問題 –

+0

優秀。在我寫下之前,我沒有注意到這是你的第一天和第一個問題。我很高興你採納了這個建議!回頭見...? ;-) –

回答

1

看看這個例子,

public class MainExpand extends ExpandableListActivity { 

    private ExpandableListAdapter adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     List<Map<String, String>> parent = new ArrayList<Map<String,String>>(); 
     List<List<Map<String, String>>> childGroup = new ArrayList<List<Map<String,String>>>(); 

     for (int i = 0; i < 5; i++) { 

      Map<String, String> parentMap = new HashMap<String, String>(); 
      parentMap.put("lalit", "Parent "+i); 
      parent.add(parentMap); 

      List<Map<String, String>> child = new ArrayList<Map<String,String>>(); 
      for (int j = 0; j < 2; j++) { 

       Map<String, String> childMap = new HashMap<String, String>(); 
       childMap.put("lalit", "Child "+j); 
       child.add(childMap); 
      } 
      childGroup.add(child); 
     } 

     adapter = new SimpleExpandableListAdapter(
       this, 
       parent,android.R.layout.simple_expandable_list_item_1,new String[] {"lalit"},new int[] { android.R.id.text1, android.R.id.text2}, 
       childGroup,android.R.layout.simple_expandable_list_item_2,new String[]{"lalit","even"}, new int[]{android.R.id.text1,android.R.id.text2} 
       ); 
     setListAdapter(adapter); 
    } 
0

這是你的兩個問題的答案。如果有什麼不適合你,請在評論中告訴我。

的第1部分答案):

請看這裏:

http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html

onChildClick(ExpandableListView父視圖V:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html

private String[][] children = { 
      { "Academic1", "Academic2", "Academic3", "Academic4" }, 
      { "Dining1", "Dining2", "Dining2", "Dining3" }, 
      { "Residential1", "Residential2" }, 
    }; 

的第2部分)答案,int groupPosition,int childPosition,long id)Callb ack方法將在 這個可擴展列表中的一個孩子被點擊時被調用。

1

我同時使用你的答案的組合。謝謝。我採用了多維數組的優勢。

首先我創建了這些數組。

private String[] buildingTypes = { 
      "Academic Buildings", "Dining Commons", "Residential Halls" }; 

private String[][] buildingList = { 
      { "Agriculutural Engineering", "Army ROTC", "Arnold House", "Studio Arts Bldg","Bartlett" }, 
      { "Berkshire", "Franklin", "Hampden", "Hampshire", "Worcester" }, 
      { "Baker Hall", "Brett Hall", "Brooks Hall", "Brown Hall","Butterfield Hall" }, 

然後我修改了for循環

for (int i = 0; i < buildingTypes.length; i++) { 

      Map<String, String> parentMap = new HashMap<String, String>(); 
      parentMap.put("lalit", buildingTypes[i]); 
      parent.add(parentMap); 

      List<Map<String, String>> child = new ArrayList<Map<String,String>>(); 
      for (int j = 0; j < rows; j++) { 

       Map<String, String> childMap = new HashMap<String, String>(); 
       childMap.put("lalit", buildingList[i][j]); 
       child.add(childMap); 
      } 
      childGroup.add(child); 
     } 
相關問題