我想創建帶有卡片視圖上的編輯和刪除選項的彈出式菜單我已經嘗試了下面的代碼,但我在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 –