본문 바로가기

Android

항목 뷰 - 확장리스트뷰

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <ExpandableListView

        android:id="@+id/list"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

 

</LinearLayout>

 

package com.example.expandablelistviewex01;

 

import java.util.ArrayList;

 

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.ExpandableListActivity;

import android.view.Menu;

import android.widget.ExpandableListAdapter;

import android.widget.ExpandableListView;

import android.widget.SimpleExpandableListAdapter;

 

public class MainActivity extends Activity {

     ExpandableListView mList;

     String[] arProv = new String[]{"충청도","경기도","강원도"};

     String[][] arCity = new String[][]{

                {"논산","당진","부여"},{"수원","용인"},

                {"춘천","원주","홍천","강릉"},

     };

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

           mList=(ExpandableListView)findViewById(R.id.list);

           List<Map<String,String>>provData =

                     new ArrayList<Map<String,String>>();

           List<List<Map<String, String>>> cityData =

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

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

                Map<String, String> Prov = new HashMap<String, String>();

                Prov.put("prov", arProv[i]);

                provData.add(Prov);

                List<Map<String, String>>children =

                           new ArrayList<Map<String,String>>();

                for(int j =0; j<arCity[i].length; j++){

                     Map<String, String> City = new HashMap<String, String>();

                     City.put("city", arCity[i][j]);

                     children.add(City);

                }

                cityData.add(children);

           }

          

     ExpandableListAdapter adapter =

                new SimpleExpandableListAdapter(this, provData, android.R.layout.simple_list_item_1, new String[]{"prov"}, new int[]{android.R.id.text1},

                                                          cityData, android.R.layout.simple_list_item_1, new String[]{"city"}, new int[]{android.R.id.text1});

     mList.setAdapter(adapter);

     }

    

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}