ExpandableListActivity

来源:互联网 发布:mac toast and butter 编辑:程序博客网 时间:2024/06/05 21:13
package com.cardroid.apk;

import android.app.ExpandableListActivity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CursorTreeAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

public class ApkActivity extends ExpandableListActivity {
    private final String TAG = "Expandable1";
    ExpandableListView mExpandableList;
    ExpandableListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the expandable list view object
        mExpandableList = getExpandableListView();
        // Set our expandable list adapter
        String[] projection = new String[] { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DISPLAY_NAME };
        Cursor contactData = managedQuery(
                // Phone.CONTENT_URI,
                Uri.parse("content://media/external/audio/media"), projection,
                null, null, null);
        // 注意这里也只设置了一级项的数据
        mAdapter = new CursorTreeAdapterExample(contactData, this);
        setListAdapter(mAdapter);
        init();

        Toast.makeText(this, "Total contacts: " + contactData.getCount(),
                Toast.LENGTH_LONG);
        Log.d("EXPANDABLE", "Total contacts: " + contactData.getCount());
    }

    private void init()
    {
        mExpandableList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo info) {

            // TODO Auto-generated method stub
            menu.setHeaderTitle("aaaa");
            menu.add(0, 1, 0, "open");
            menu.add(0, 2, 6,"properties");
            }
            });
        mExpandableList.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView ada, View view,int index, long longIndex) {
                mExpandableList.showContextMenu();
            return true;
            }
            });
    }
    /**
     * Adapter implementation
     *
     * @author wk.kho
     *
     */
    public class CursorTreeAdapterExample extends CursorTreeAdapter {
        private int mGroupIdColumnIndex;
        private LayoutInflater mInflater;

        // 注意这里的游标是一级项的
        public CursorTreeAdapterExample(Cursor cursor, Context context) {
            super(cursor, context);

            mGroupIdColumnIndex = cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
            mInflater = (LayoutInflater) context
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
        }

        // 注意这里的游标是二级项的
        @Override
        protected void bindChildView(View view, Context context, Cursor cursor,
                boolean isExpanded) {
            // Bind the related data with this child view
            ((TextView) view).setText(cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
        }

        @Override
        protected void bindGroupView(View view, Context context, Cursor cursor,
                boolean isExpanded) {
            // Bind the related data with this group view
            ((TextView) view).setText(cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
        }

        // 注意这里通过一次数据库查询才得到了二级项的数据
        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            Uri.Builder builder = Uri.parse(
                    "content://media/external/audio/media").buildUpon();
            ContentUris.appendId(builder,
                    groupCursor.getLong(mGroupIdColumnIndex));

            Uri phoneNumbersUri = builder.build();

            // The returned Cursor MUST be managed by us, so we use Activity's
            // helper
            // functionality to manage it for us.
            return managedQuery(phoneNumbersUri, new String[] {
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.DISPLAY_NAME }, null, null, null);
        }

        @Override
        protected View newChildView(Context context, Cursor cursor,
                boolean isExpanded, ViewGroup parent) {
            Log.d(TAG, "newChildView");

            TextView view = (TextView) mInflater.inflate(
                    android.R.layout.simple_expandable_list_item_1, parent,
                    false);

            view.setText(" ("
                    + cursor.getPosition()
                    + ") "
                    + cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));

            return view;
        }

        @Override
        protected View newGroupView(Context context, Cursor cursor,
                boolean isExpanded, ViewGroup parent) {
            Log.d(TAG, "newGroupView");
            TextView view = (TextView) mInflater.inflate(
                    android.R.layout.simple_expandable_list_item_1, parent,
                    false);
            view.setText(" ("
                    + cursor.getPosition()
                    + ") "
                    + cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));

            return view;
        }
    }
}
原创粉丝点击