Click event from Adapter to Activity or Fragment in Android
Asked Answered
P

1

1

I have customized expandable list view showing in Navigation Drawer. I have a click listener in Adapter I would like to send click event from adapter to my activity or fragment.

Is this possible if so how do I go about doing it?

Here is a snippet of my adapter:

public class CustomAdapter implements ExpandableListAdapter {

    @Override
    public View getGroupView(int position, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.menu_item, parent, false);
        }

        TextView text = (TextView) convertView.findViewById(android.R.id.text1);
        text.setText(items.get(position).getName());
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageIndicator);

        if (items.get(position).isHasChild() && items.get(position).getSubMenuItems().size() > 0)
            imageView.setVisibility(View.VISIBLE);
        else
            imageView.setVisibility(View.INVISIBLE);

        if (isExpanded)
            imageView.setImageResource(R.drawable.down);
        else
            imageView.setImageResource(R.drawable.next);

        return convertView;
    }

    @Override
    public View getChildView(final int childPosition, int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            view = inflater.inflate(R.layout.expandable_list, parent, false);
        }
        ExpandableHeightListView childList = (ExpandableHeightListView) view.findViewById(R.id.childList);
        childList.setAdapter(new CustomAdapter(context, items.get(childPosition).getSubMenuItems()));
        childList.invalidate();
        parent.invalidate();

        final ExpandableHeightListView list = childList;
        childList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            int previousItem = -1;

            @Override
            public void onGroupExpand(int groupPosition) {
                ArrayList<eMenuItem> tempItems = items.get(childPosition).getSubMenuItems();

                if (tempItems.get(groupPosition).getSubMenuItems().size() == 0) {
                    Toast.makeText(context, "Start Activity", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Clicked on " + tempItems.get(groupPosition).getName(), Toast.LENGTH_SHORT).show();
                }

                if (groupPosition != previousItem)
                    list.collapseGroup(previousItem);

                previousItem = groupPosition;
                list.invalidate();
                parent.invalidate();
            }
        });
        return view;
    }
}

WITH HELP FROM Jared Burrows - How to create interface between Fragment and adapter?

Added few things to my CustomAdapter and FragmentActivity

public class CustomAdapter implements ExpandableListAdapter {

public CustomAdapter(MainActivity fragmentactivity) 
{
    try 
    {
        this.mAdapterCallback = ((AdapterCallback) fragmentactivity);
    } 
    catch (ClassCastException e) 
    {
        throw new ClassCastException("Fragment must implement AdapterCallback.");
    }
}

public static interface AdapterCallback 
{
    void onMethodCallback();
}

    @Override
    public View getGroupView(int position, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.menu_item, parent, false);
        }

        TextView text = (TextView) convertView.findViewById(android.R.id.text1);
        text.setText(items.get(position).getName());
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageIndicator);

        if (items.get(position).isHasChild() && items.get(position).getSubMenuItems().size() > 0)
            imageView.setVisibility(View.VISIBLE);
        else
            imageView.setVisibility(View.INVISIBLE);

        if (isExpanded)
            imageView.setImageResource(R.drawable.down);
        else
            imageView.setImageResource(R.drawable.next);

        return convertView;
    }

    @Override
    public View getChildView(final int childPosition, int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            view = inflater.inflate(R.layout.expandable_list, parent, false);
        }
        ExpandableHeightListView childList = (ExpandableHeightListView) view.findViewById(R.id.childList);
        childList.setAdapter(new CustomAdapter(context, items.get(childPosition).getSubMenuItems()));
        childList.invalidate();
        parent.invalidate();

        final ExpandableHeightListView list = childList;
        childList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            int previousItem = -1;

            @Override
            public void onGroupExpand(int groupPosition) {
                ArrayList<eMenuItem> tempItems = items.get(childPosition).getSubMenuItems();

                if (tempItems.get(groupPosition).getSubMenuItems().size() == 0) {
                    Log.e("value","position"+childPosition);
                Log.e("value","position"+groupPosition);
                Log.e("value","position"+tempItems.get(groupPosition).getName());

                mAdapterCallback.onMethodCallback();

                } else {
                    Toast.makeText(context, "Clicked on " + tempItems.get(groupPosition).getName(), Toast.LENGTH_SHORT).show();
                }

                if (groupPosition != previousItem)
                    list.collapseGroup(previousItem);

                previousItem = groupPosition;
                list.invalidate();
                parent.invalidate();
            }
        });
        return view;
    }
}

AND IN MY MAIN ACTIVITY:

public class MainActivity extends FragmentActivity implements AdapterCallback
{
 @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    this.mMyAdapter = new CustomAdapter(this);
   }

And then added this outside Oncreate:

public void onMethodCallback() 
{
        Log.e("Call","VALUE");
 }
 }

but I am getting the NPE Error still.. This is strange when I don't use the callback I am not getting NPE but when I use the callback I am getting NPE: http://pastebin.com/wc2ByiFz

Paisley answered 17/3, 2015 at 6:45 Comment(7)
is your adapter in a seperate class?Crosshead
Wait i am writing the solutionArabel
Just make the adapter an inner class of your fragment or activityCrosshead
Now that you updated this question, where is at com.ylg.link.CustomAdapter$1.onGroupExpand(CustomAdapter.java:149)?Karlakarlan
Exactly the place where I am calling the call back.. after the three logs.Paisley
Always check for null or try exception just like my example. if (mAdapterCallback.onMethodCallback != null ) { mAdapterCallback.onMethodCallback(); }. Please pay closer attention to my example as it works.Karlakarlan
Added try and catch. I still get NPE issue in exception. I am not able to get the results.Paisley
A
0
   public class CustomAdapter implements ExpandableListAdapter {

    Activity yourActivity
     public CustomAdapter(Activity yourActivity)
                    {

            this.yourActivity= yourActivity;

                }

    @Override
    public View getGroupView(int position, boolean isExpanded, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.menu_item, parent, false);
        }

        TextView text = (TextView) convertView.findViewById(android.R.id.text1);
        text.setText(items.get(position).getName());
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageIndicator);

        if (items.get(position).isHasChild() && items.get(position).getSubMenuItems().size() > 0)
            imageView.setVisibility(View.VISIBLE);
        else
            imageView.setVisibility(View.INVISIBLE);

        if (isExpanded)
            imageView.setImageResource(R.drawable.down);
        else
            imageView.setImageResource(R.drawable.next);

        return convertView;
    }

    @Override
    public View getChildView(final int childPosition, int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            view = inflater.inflate(R.layout.expandable_list, parent, false);
        }
        ExpandableHeightListView childList = (ExpandableHeightListView) view.findViewById(R.id.childList);
        childList.setAdapter(new CustomAdapter(context, items.get(childPosition).getSubMenuItems()));
        childList.invalidate();
        parent.invalidate();

        final ExpandableHeightListView list = childList;
        childList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            int previousItem = -1;

            @Override
            public void onGroupExpand(int groupPosition) {
                ArrayList<eMenuItem> tempItems = items.get(childPosition).getSubMenuItems();

                if (tempItems.get(groupPosition).getSubMenuItems().size() == 0) {
                     yourActivity.onYourClick(groupPosition);
                } else {
                    Toast.makeText(context, "Clicked on " + tempItems.get(groupPosition).getName(), Toast.LENGTH_SHORT).show();
                }

                if (groupPosition != previousItem)
                    list.collapseGroup(previousItem);

                previousItem = groupPosition;
                list.invalidate();
                parent.invalidate();
            }
        });
        return view;
    }
}

//Your Fragment

    public class yourActivity extends FragmentActivity{
            public void onYourClick(int groupPosition){
            }
Arabel answered 17/3, 2015 at 6:59 Comment(16)
@ Ammar aly : can you please give me an example? What do you mean by onYourClick and paramenters please?Paisley
Onyourclick is the method that you will call as callback in your fragment or in activityArabel
How do I link fragment with the adpter?Paisley
It gives error the method onYourClick(int) is undefined for the type Activity?Paisley
do you have any activity make a method in itArabel
Like what do you want from it?Paisley
It is exactly the same like what you have done. listFragment.openGridViewData(tempItems.get(groupPosition).getName(), childPosition);Paisley
And in FragmentActivity: public void openGridViewData(String name, int position)Paisley
if method is there then your not calling it properly check the activity reference in constructorArabel
did you make constructor in your adapterArabel
public CustomAdapter(FragmentActivity gridActivity) { this.gridActivity = gridActivity; }Paisley
what is your class name the activity you are callingArabel
public CustomAdapter(ListFragment listFragment) { this.listFragment= listFragment; }Arabel
Yep. I added it now I am getting 03-17 13:25:58.372: E/AndroidRuntime(8210): java.lang.NullPointerException 03-17 13:25:58.372: E/AndroidRuntime(8210): at com.ylk.link.CustomAdapter$1.onGroupExpand(CustomAdapter.java:149) 03-17 13:25:58.372: E/AndroidRuntime(8210): at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:696)Paisley
Refer to this #23740734Arabel
I am not sure why this NullpinterException is coming up because I am getting the log value correctly when clicking on the item I am getting this error only when I am trying to send the gridActivity.openGridViewData(tempItems.get(groupPosition).getName(), groupPosition); - I add to casting to for Fragment.. Something like this: ((MainActivity) gridActivity).openGridViewData(tempItems.get(groupPosition).getName(), groupPosition);Paisley

© 2022 - 2024 — McMap. All rights reserved.