Click event from Adapter to Activity or Fragment in Android

↘锁芯ラ 提交于 2019-12-13 02:03:03

问题


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


回答1:


   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){
            }


来源:https://stackoverflow.com/questions/29092877/click-event-from-adapter-to-activity-or-fragment-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!