2

I am developing an app like google Play Music! the thing is like that app i have a popupmenu for each row on my gridview. I'm setting grid view adapter from fragment which implements onClickListener. question is: how can i set onclicklistener for each popupmenu and handle onclick event from fragment my popupmenu contains two items : DELETE Add to playList

the reason i want to access onClick from fragment is i want to delete the specific file and update grid view with notifydatasetchanged() !

how can i do this or any suggestion for how can i delete the specific file from adapter n update grid view!

thanks!

this is my GridView Adapter :

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;
    item = songs.get(position);

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(com.irangrammy.irangrammy.R.layout.mchannel_row, parent, false);
        holder = new ViewHolder();
        holder.thumbnail = (ImageView) row.findViewById(com.irangrammy.irangrammy.R.id.image);
        holder.title = (TextView) row.findViewById(com.irangrammy.irangrammy.R.id.singer);
        holder.mMenu = (ImageView) row.findViewById(R.id.itemOption);

        holder.mMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                PopupMenu popUp = new PopupMenu(context, v);
                MenuInflater inflater = popUp.getMenuInflater();
                inflater.inflate(R.menu.mchannel_popup, popUp.getMenu());
                popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem arg0) {
                        // TODO Auto-generated method stub
                        if(arg0.getItemId()==R.id.delete)
                        {

                        }
                    return true;
                    }
                });
                popUp.show();       
            }
        });
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
Mahdi Giveie
  • 630
  • 1
  • 8
  • 25
  • 1
    use interface or pass your fragment to Adapter class – Shayan Pourvatan Dec 06 '14 at 09:58
  • @shayanpourvatan i pass fragment to my adapter class , but it doesn't work! can u give me example how to pass fragment to adapter n retrieve it in adapter class? – Mahdi Giveie Dec 06 '14 at 10:05
  • 1
    pass your fragment to Adapter class , instead of creating new OnClickListener cast your fragment to onClickListener, i'm not sure about that but you can try that – Shayan Pourvatan Dec 06 '14 at 10:17
  • @shayanpourvatan i pass fragment which implements onCLickListener by this, n retrieve it in adapter class but get this error when cast it : The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (Fragment) – Mahdi Giveie Dec 06 '14 at 10:23
  • 1
    cast your fragment to OnClickListener – Shayan Pourvatan Dec 06 '14 at 10:24
  • @shayanpourvatan how to cast? i use setOnclickListener(myFragment) but get that error – Mahdi Giveie Dec 06 '14 at 10:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66319/discussion-between-mahdi-giveie-and-shayan-pourvatan). – Mahdi Giveie Dec 06 '14 at 11:01

3 Answers3

2

you can pass your fragment to Adapter class and cast your fragment to onClickListener, but be sure that you have implement onClickListener on Fragment class.

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
1

I haven't get you! but if I understand you the Solution: Try think of sending broadcast to your fragment, and when the fragment get the broadcast, deal with it as you want. UPDATE: if you want to set listener to the view you can do it:

convertView.setOnClickListener(new View.ClickListener() {
                    @Override
                    public boolean onLongClick(View view) {

                     // Do what you want when the view clicked!
                    }
                });

If this not the answer - send me message with your native language

Esmaeel Ibraheem
  • 316
  • 2
  • 14
1

first of all your View holder design pattern is wrong it must be like below:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;
    item = songs.get(position);

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(com.irangrammy.irangrammy.R.layout.mchannel_row, parent, false);
        holder = new ViewHolder();
        holder.thumbnail = (ImageView) row.findViewById(com.irangrammy.irangrammy.R.id.image);
        holder.title = (TextView) row.findViewById(com.irangrammy.irangrammy.R.id.singer);
        holder.mMenu = (ImageView) row.findViewById(R.id.itemOption);


        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

        holder.mMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                PopupMenu popUp = new PopupMenu(context, v);
                MenuInflater inflater = popUp.getMenuInflater();
                inflater.inflate(R.menu.mchannel_popup, popUp.getMenu());
                popUp.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem arg0) {
                        // TODO Auto-generated method stub
                        if(arg0.getItemId()==R.id.delete)
                        {
                           songs.get(position).remove();
                           notifyDataSetChanged();

                        }
                    return true;
                    }
                });
                popUp.show();       
            }
        });
}
mmlooloo
  • 18,937
  • 5
  • 45
  • 64