2013-07-14 42 views
1

我有一個可工作的expandablelistview代碼,它可以獨立工作。 我有另一個工作代碼滑動選項卡視圖,我分別寫了。片段中的可擴展列表視圖

這些都是通過一些博客,android dev和stackoverflow問題後寫的。

當我嘗試將可擴展列表視圖合併到一個片段中時,最終出現錯誤,我無法解決。

下面是來自片段一塊代碼:

public class Fragment1 extends Fragment { 
private static final String NAME = "NAME"; 
private static final String IS_EVEN = "IS_EVEN"; 

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 < 10; i++) { 
     Map<String, String> curGroupMap = new HashMap<String, String>(); 
     groupData.add(curGroupMap); 
     curGroupMap.put(NAME, "Group " + i); 
     curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd"); 

     List<Map<String, String>> children = new ArrayList<Map<String, String>>(); 
     for (int j = 0; j < 2; j++) { 
      Map<String, String> curChildMap = new HashMap<String, String>(); 
      children.add(curChildMap); 
      curChildMap.put(NAME, "Child " + j); 
      curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd"); 
     } 
     childData.add(children); 
    } 

    // Set up our adapter 
    mAdapter = new SimpleExpandableListAdapter(
      getActivity(), 
      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); 
} 

}` 

總是收到錯誤上的最後一行:「的方法setListAdapter(ExpandableListAdapter)是未定義的類型片段1」。

其餘代碼顯示沒有錯誤。

請幫忙。我該如何糾正這個問題。

+0

可能會對你有所幫助https://gist.github.com/mosabua/1316903 –

+0

謝謝..我試過這個例子,但是我沒有得到任何listview,當我在AVD上運行它時,它給出了一個混合屏幕分段。 – Ramanathan

回答

5

我自己能夠解決這個問題。爲了將來的參考,這裏是代碼。可能很麻煩,需要清理,但它現在可行!

public class HotDeals extends Fragment { 
private ExpandableListAdapter mAdapter; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle    savedInstanceState) { 
    View v = inflater.inflate(R.layout.saved_tabs, null); 

return v; 
} 

private static final String NAME = "NAME"; 
private static final String IS_EVEN = "IS_EVEN"; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(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 < 10; i++) { 
     Map<String, String> curGroupMap = new HashMap<String, String>(); 
     groupData.add(curGroupMap); 
     curGroupMap.put(NAME, "Group " + i); 
     curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd"); 

     List<Map<String, String>> children = new ArrayList<Map<String, String>>(); 
     for (int j = 0; j < 2; j++) { 
      Map<String, String> curChildMap = new HashMap<String, String>(); 
      children.add(curChildMap); 
      curChildMap.put(NAME, "Child " + j); 
      curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This   child  is odd"); 
     } 
     childData.add(children); 
    } 
    ExpandableListView lv = (ExpandableListView) getActivity().findViewById(R.id.list); 
    // Set up our adapter 
    mAdapter = new SimpleExpandableListAdapter(
      getActivity(), 
      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 } 
      ); 
    lv.setAdapter(mAdapter); 
} 
} 

@Divers & @Dixit - Thanx。

0

Fragment不包含方法setListAdapterListFragment做。你必須從這個類擴展你的片段來解決這個問題。

+1

試過ListFragment,我得到的錯誤是:'類型ListFragment中的方法setListAdapter(ListAdapter)不適用於參數(ExpandableListAdapter)'。 – Ramanathan

0

這篇文章已經快2年了,但在這個頁面中沒有任何答案是相關的。

回答PO問題,

方法setListAdapter(ExpandableListAdapter)是未定義的類型片段1

這是因爲setListAdapter是ListFragment或ListActivity的方法,它正在等待對於ListAdapter或其子類作爲參數。你得到這個錯誤是因爲你給了它一個ExpandableListAdapter,它沒有實現ListAdapter接口。

希望這會幫助別人。

+0

是的,但這並不回答「我怎樣才能糾正這個問題?」 – Damnum