简体   繁体   中英

Android Pass Value from Fragment to Adapter Class

I have below code to show popup menu when user click on the overflow image in each row item. Unfortunately i have no idea how to pass the context to the fragment which caused the onClick() didnt get the context value. I managed to show the popup menu if i create the listener directly in the getView() of the adapter class but i cant get the expected result if i passed the listener to the fragment. May i know how can i adapt my code below?

My current coding is as below:

public class SubProductCustomAdapter extends BaseAdapter {
 ...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //RecyclerView (Android 5.0) - To avoid app crash when there are too many records when user scroll
    ViewHolder viewHolder;
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    //First Created
    if(convertView == null )
    {
        convertView = mInflater.inflate(R.layout.item_sub_product, null);
        viewHolder = new ViewHolder();

        viewHolder.sub_product_name = (TextView)convertView.findViewById(R.id.txt_pdt_name);
        viewHolder.popup_menu = (ImageView) convertView.findViewById(R.id.product_overflow);

        SubRowProducts subRowPro = subRowProducts.get(position);
        viewHolder.sub_product_name.setText(subRowPro.getSub_product_name());

        //Managed to display popup menu using below method
        /*viewHolder.popup_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(context,v);

                popupMenu.getMenuInflater().inflate(R.menu.drawermenu, popupMenu.getMenu());

                popupMenu.show();
            }
        });*/

        //Not managed to display popup menu using below method
        viewHolder.popup_menu.setOnClickListener(new MySubProductListFragment());

        //Store object inside convert view
        convertView.setTag(viewHolder);

    }

    else{
        //Reused or recycle the convertView
        viewHolder = (ViewHolder)convertView.getTag();
    }

    //viewHolder.sub_product_name.setText(subRowProducts.get(position).getSub_product_name());

    return convertView;
}

.

************************************************************************************************************

public class MySubProductListFragment extends ListFragment implements View.OnClickListener {

...

 @Override
public void onClick(View v) {

    PopupMenu popupMenu = new PopupMenu(getActivity(),v);

    popupMenu.getMenuInflater().inflate(R.menu.drawermenu, popupMenu.getMenu());

    popupMenu.show();

}

}

  1. Define a interface in your adapter class with required methods.
  2. Have a adapter constructor which expects the reference for that interface
  3. Now in let your ListFragment class implement the adapter's interface , and when you create a reference for your adapter class pass this as the parameter to adapter class constructor.
  4. From adapter class constructor have a private reference of fragment, and call which ever method you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM