我想创建带有卡片视图上的编辑和删除选项的弹出式菜单我已经尝试了下面的代码,但我在popup.show();
处收到强制关闭,致命异常onclick
的imageview
。我应该怎么做才能在cardview
上创建弹出菜单?如何在android卡片视图中创建弹出式菜单
public class ShipRecyclerAdapter extends RecyclerView.Adapter<ShipRecyclerAdapter.ViewHolder> {
public static final String PRODUCTID = "PRODUCTID";
public static final String PRODUCTNAME = "PRODUCTNAME";
List<CustDatabaseModel> dbCustList;
static Context context;
public ImageView btndots;
ShipRecyclerAdapter(Context context, List<CustDatabaseModel> dbCustList){
this.dbCustList = new ArrayList<CustDatabaseModel>();
this.context = context;
this.dbCustList = dbCustList;
}
@Override
public ShipRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.address_list, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ShipRecyclerAdapter.ViewHolder holder, final int position) {
holder.address.setText(dbCustList.get(position).getaddress());
holder.city.setText(dbCustList.get(position).getcity());
holder.state.setText(dbCustList.get(position).getstate());
holder.zipcode.setText(dbCustList.get(position).getzip());
final String adr=dbCustList.get(position).getaddress().toString();
holder.btndots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
PopupMenu popup = new PopupMenu(context, btndots);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent i;
if ((item.getTitle().toString()).equalsIgnoreCase("Edit")) {
Toast.makeText(ShipRecyclerAdapter.context, "you have clicked Menu image ", Toast.LENGTH_LONG).show();
} else if ((item.getTitle().toString()).equalsIgnoreCase("Delete")) {
Toast.makeText(ShipRecyclerAdapter.context, "you have clicked Menu image ", Toast.LENGTH_LONG).show();
}
return true;
}
});
popup.show();
}
});
//closing the setOnClickListener method
}
@Override
public int getItemCount() {
return dbCustList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView state,address,city,zipcode;
public ImageView btndots;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
address = (TextView) itemLayoutView.findViewById(R.id.textViewAddress);
city = (TextView)itemLayoutView.findViewById(R.id.textViewCity);
zipcode = (TextView)itemLayoutView.findViewById(R.id.textViewZipcode);
state = (TextView)itemLayoutView.findViewById(R.id.textViewState);
this.btndots = (ImageView)itemLayoutView.findViewById(R.id.cmenu);
}
}
}`
您的问题不清楚,您正在讨论弹出菜单,并显示适配器的代码。弹出式菜单 –
的代码在哪里,请参阅此堆栈文档:http://stackoverflow.com/documentation/android/169/recyclerview/14542/popup-menu-with-recyclerview#t=201611030914370847887 –