Reputation: 63
I have a recyclerview where there is an image for a popup menu in recylerview item (see the image below), so I created a popup menu with edit and delete option on cardView I have tried the below code but when I click on the popup menu button, nothing's happening In my NotesAdapter I want to retrieve data
Here is my item.xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
android:layout_margin="5dp"
app:cardCornerRadius="10dp"
app:cardBackgroundColor="@android:color/holo_red_dark"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="90dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_marginStart="5dp"
android:textColor="@android:color/white"
android:textSize="25sp"
android:text="Title"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"/>
<ImageView
android:id="@+id/menuIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:clickable="true"
app:srcCompat="@drawable/ic_popup_menu" />
</RelativeLayout>
<TextView
android:layout_margin="5dp"
android:textSize="15sp"
android:textColor="@android:color/white"
android:text="Section"
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Here is my NotesAdapter.java
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyHolder>
{
List<Listdata> noteslist;
private Context context;
public NotesAdapter(List<Listdata> noteslist,Context context)
{
this.context=context;
this.noteslist=noteslist;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
MyHolder myHolder=new MyHolder(view);
return myHolder;
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int position) {
final Listdata data=noteslist.get(position);
myHolder.title.setText(data.getTitle());
myHolder.desc.setText(data.getDesc());
}
@Override
public int getItemCount() {
return noteslist.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView title,desc;
public MyHolder(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
desc=itemView.findViewById(R.id.desc);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Listdata listdata=noteslist.get(getAdapterPosition());
Intent i=new Intent(context, StreamActivity.class);
i.putExtra("id",listdata.id);
i.putExtra("title",listdata.title);
i.putExtra("desc",listdata.desc);
context.startActivity(i);
}
});
ImageView menuIcon = itemView.findViewById(R.id.menuIcon);
menuIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu menu = new PopupMenu(v.getContext(), v);
menu.setGravity(Gravity.END);
menu.getMenu().add("Edit").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Listdata listdata=noteslist.get(getAdapterPosition());
Intent i=new Intent(context, EditActivity.class);
i.putExtra("id",listdata.id);
i.putExtra("title",listdata.title);
i.putExtra("desc",listdata.desc);
context.startActivity(i);
return false;
}
});
menu.getMenu().add("Delete").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
}
});
}
}
}
Upvotes: 1
Views: 1487
Reputation: 371
Try this
@Override
public void onBindViewHolder(@NonNull final MyHolder myHolder, final int position) {
final Listdata data=noteslist.get(position);
myHolder.title.setText(data.getTitle());
myHolder.desc.setText(data.getDesc());
myHolder.menuIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, myHolder.menuIcon);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
Toast.makeText(context, "Edit", Toast.LENGTH_SHORT).show();
Intent i=new Intent(context, EditActivity.class);
i.putExtra("id",data.id);
i.putExtra("title",data.title);
i.putExtra("desc",data.desc);
context.startActivity(i);
break;
case R.id.action_delete:
noteslist.remove(position);
notifyDataSetChanged();
Toast.makeText(context, "delete", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
});
popupMenu.show();
}
});
}
@Override
public int getItemCount() {
return noteslist.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView title,desc;
ImageView menuIcon;
public MyHolder(@NonNull View itemView) {
super(itemView);
menuIcon = itemView.findViewById(R.id.menuIcon);
title=itemView.findViewById(R.id.title);
desc=itemView.findViewById(R.id.desc);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Listdata listdata=noteslist.get(getAdapterPosition());
Intent i=new Intent(context, StreamActivity.class);
i.putExtra("id",listdata.id);
i.putExtra("title",listdata.title);
i.putExtra("desc",listdata.desc);
context.startActivity(i);
}
});
}
}
Upvotes: 4
Reputation: 379
Hi i have seen in your code that you are just creating popup menu object but not showing to show a popup menu follow below code.
private void showPopupMenu(View view) {
// inflate menu
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_album, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
Upvotes: 0