Android – Arraylist Adapter Örneği

Aşağıdaki örnek kodla ExpandableListActivity i extend ederek oluşturacağınız basit bir list activity kod parçacığı var. Hope it works.


package com.mehmetfasil;

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

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

public class BasitExpandableListAdapterActivity extends ExpandableListActivity {
    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> groupData = new ArrayList<Map>();
        List<List<Map>> childData = new ArrayList<List<Map>>();
        for (int i = 0; i < 20; i++) {
            Map curGroupMap = new HashMap();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, "Item " + i);
            curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "Even grubu Burası" : "Odd grubu da burası");

            List<Map> children = new ArrayList<Map>();
            for (int j = 0; j < 5; j++) {
                Map curChildMap = new HashMap();
                children.add(curChildMap);
               // curChildMap.put(NAME, "Child " + j);
                curChildMap.put(IS_EVEN, (j % 2 == 0) ? "Selam " + j: "Günaydın "+ j);
            }
            childData.add(children);
        }

        // 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);
    }

}