gpt4 book ai didi

android - 如何在android cardview中创建弹出菜单

转载 作者:太空狗 更新时间:2023-10-29 16:00:26 26 4
gpt4 key购买 nike

我想在卡片 View 上创建带有编辑和删除选项的弹出菜单我已经尝试了下面的代码但是我在 popup.show(); 处强制关闭致命异常(exception) onclickimageview .我应该怎么做才能在 cardview 上创建弹出菜单? ?

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);


}



}

}`

最佳答案

首先像这样在菜单文件夹中创建xml文件,

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/edit"
android:title="@string/edit"
app:showAsAction="ifRoom" />

<item
android:id="@+id/delete"
android:title="@string/delete"
app:showAsAction="ifRoom" />

</menu>

然后在你的适配器类中创建方法,

private void showPopup(View view, final int position) {
// pass the imageview id
View menuItemView = view.findViewById(R.id.btn_song_list_more);
PopupMenu popup = new PopupMenu(activity, menuItemView);
MenuInflater inflate = popup.getMenuInflater();
inflate.inflate(R.menu.popup_menu, popup.getMenu());
Log.e("position -- " + position);

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
// do what you need.
break;
case R.id.delete:
// do what you need .
break;
default:
return false;
}
return false;
}
});
popup.show();
}

call this method of your imageview click event.

关于android - 如何在android cardview中创建弹出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35718635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com