get On Item Selected Listener for AdapterView - Android User Interface

Android examples for User Interface:AdapterView

Description

get On Item Selected Listener for AdapterView

Demo Code


//package com.java2s;
import java.lang.reflect.Field;
import android.util.Log;

import android.widget.AdapterView;

public class Main {
    public static AdapterView.OnItemSelectedListener getOnItemSelectedListener(
            AdapterView view) {//from w  w w.  ja  v  a  2 s.  co m
        AdapterView.OnItemSelectedListener listener = null;
        try {
            Field listenerField = null;
            listenerField = getSuperClassOfType(view.getClass(),
                    AdapterView.class.getName()).getDeclaredField(
                    "mOnItemSelectedListener");
            listenerField.setAccessible(true);
            listener = (AdapterView.OnItemSelectedListener) listenerField
                    .get(view);
        } catch (Exception ex) {
            listener = null;
        }
        return listener;
    }

    public static Class getSuperClassOfType(Class clazz,
            String superClassName) {
        Class tempClass = clazz;
        while (tempClass != null
                && !tempClass.getName().equals(superClassName))
            tempClass = tempClass.getSuperclass();

        Log.i("Recorder", tempClass == null ? "null" : tempClass.getName());

        return tempClass;
    }
}

Related Tutorials