2012年5月29日火曜日

開閉ListView

ListViewのオプションであんだろーと思いきや別ビューだった、残念

ということで使うのはExpandableListViewとやら。
親→子の概念を持ったリスト。
adapterセットに使う引数は驚きの9個!9個ですよ奥さん!

(ttp://dev.classmethod.jp/smartphone/basic-android-component-03-expandablelistview/)
を全面的に参考。

親と子の定義。



    ArrayList<HashMap<String, String>> parent
         = new ArrayList<HashMap<String,String>>();

    ArrayList<ArrayList<HashMap<String, String>>> child
         = new ArrayList<ArrayList<HashMap<String,String>>>();

親グループ定義。ここでは一個だけ。


    HashMap<String, String> group1 = new HashMap<String, String>();
    groupA.put("group", "成人病");


ぶち込み。

parent.add(group1)


子定義。


  ArrayList<HashMap<String, String>> childList = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> child1 = new HashMap<String, String>();
    child1.put("group", "成人病");
    child1.put("name", "肝硬変");
    HashMap<String, String> child2 = new HashMap<String, String>();
    child2.put("group", "成人病");
    child2.put("name", "糖尿");


子と親の結びつけはキーで結ぶ。ここで言うgroup。
ぶち込み。

childList.add(child1)
childList.add(child2)

listをぶち込み。

childData.add(childList)


adapter。


       SimpleExpandableListAdapter Exadapter1 = new SimpleExpandableListAdapter(
               getApplicationContext(),
               groupData,
               android.R.layout.simple_expandable_list_item_1,
               new String[] {"group"},
               new int[] { android.R.id.text1 },
               childData,
               R.layout.onedata,
               new String[] {"name"},
               new int[] { R.id.rowtext1});

親と子で使うレイアウト変えてるけどまぁこんな感じ。
コンテキストのあとはデータ、レイアウト、キー値、レイアウトのリソースIDの順。
子要素の数だけhashmapをnewする必要があるのでDB絡める実装はちょい考え中。
うーん。



0 件のコメント:

コメントを投稿