这是我在holder.cart
上的代码我设置onclick
函数来获取弹出菜单,但我在行中得到空指针异常
PopupMenu popup = new PopupMenu(mContext, view);
showPopupMenu(holder.cart);
任何人都可以通过点击holder解决这个问题。我应该显示一个弹出菜单布局,但在这里我使用了menuinflater,我想将其更改为布局inflater并显示弹出视图。
import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.MenuInflater;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class RecyclerAdapterProducts extends RecyclerView.Adapter<RecyclerAdapterProducts.ViewHolder> {
//ArrayList<Products> products;
OnItemClickListener clickListener;
ArrayList<ImageAndName> imageAndNames;
private Context mContext;
//byte[] imz;
public RecyclerAdapterProducts(ArrayList<ImageAndName> imageAndNames){
this.imageAndNames=imageAndNames;
//this.discounts=discounts;
}
/*public SimpleRecyclerAdapter(ArrayList<Products> products){
this.products=products;
}*/
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
/*public TextView name,offer;
public TextView price;*/
public ImageView im;
public TextView name;
public TextView part;
public ImageButton cart;
public int position;
public ViewHolder(View v) {
super(v);
name = (TextView)v.findViewById(R.id.code);
part = (TextView)v.findViewById(R.id.textView11);
part.setTypeface(null, Typeface.BOLD_ITALIC);
im = (ImageView)v.findViewById(R.id.productImg);
v.setOnClickListener(this);
cart = (ImageButton)v.findViewById(R.id.imageView2);
/*cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
*/
/*// LayoutInflater inflater = LayoutInflater.from(RecyclerAdapterProducts.this);
View popupView = LayoutInflater.from(mContext).inflate(R.layout.quantityalertdialog, null);
final EditText subEditText = (EditText)popupView.findViewById(R.id.dialogEditText);
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
*//*builder.setTitle("AlertDialog");
builder.setMessage("AlertDialog Message");*//*
builder.setView(popupView);
AlertDialog alertDialog = builder.create();
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();*/
//View popupView = LayoutInflater.from(mContext).inflate(R.layout.quantityalertdialog, null);
/* }
});*/
//price = (TextView)v.findViewById(R.id.serviceDate);
//offer=(TextView)v.findViewById(R.id.offer);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(v, getAdapterPosition());
}
}
public RecyclerAdapterProducts(Context mContext, ArrayList<ImageAndName> imageAndNames) {
this.mContext = mContext;
this.imageAndNames = imageAndNames;
}
@Override
public RecyclerAdapterProducts.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_card_individual_product, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final RecyclerAdapterProducts.ViewHolder holder,int position) {
holder.im.setImageResource(imageAndNames.get(position).getIconImageResource());
holder.name.setText(imageAndNames.get(position).getName());
holder.part.setText(imageAndNames.get(position).getPartno());
holder.cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(holder.cart);
}
});
holder.position=position;
}
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();
}
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
public MyMenuItemClickListener() {
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_add_favourite:
Toast.makeText(mContext, "Add to Wishlist", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_play_next:
//Toast.makeText(mContext, "Play next", Toast.LENGTH_SHORT).show();
return true;
default:
}
return false;
}
}
/*final Products pd= products.get(position);
if (discounts!=null && discounts.size()>0) {
for (int i = 0; i < discounts.size(); i++) {
if (pd.getId().equals(((ArrayList)discounts.get(i)).get(0))){
ArrayList disc=(ArrayList)discounts.get(i);
holder.offer.setVisibility(View.VISIBLE);
holder.offer.setText("Buy "+disc.get(1)+" to get "+disc.get(2)+"% discount");
}
else {
holder.offer.setVisibility(View.GONE);
}
}
}
holder.name.setText(pd.getName());
holder.position=position;
holder.price.setText(pd.getList_price() + "\u20B9");
if (pd.getId()!=null) {
new AsyncTask<Integer, Void, ImageModel>() {
private ViewHolder v;
private ImageModel img;
@Override
protected ImageModel doInBackground(Integer... params) {
//v = params[0];
final String url = "http://164.164.35.38:443/seis/productTemplateController/getProductImage/"+params[0];
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
img = restTemplate.getForObject(url, ImageModel.class);
//byte[] ing=
return img;
}
@Override
protected void onPostExecute(final ImageModel result) {
//super.onPostExecute(result);.get(0).toString().getBytes()
if (result != null && result.getImage()!=null) {
//imz=result.get(0).toString().getBytes();
// If this item hasn't been recycled already, hide the
// progress and set and show the image
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
Bitmap bm = BitmapFactory.decodeByteArray(android.util.Base64.decode(result.getImage(), 0), 0, android.util.Base64.decode(result.getImage(), 0).length);
return bm;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (v.position == position) {
// If this item hasn't been recycled already, hide the
// progress and set and show the image
v.im.setImageBitmap(result);
}
}
}.execute(holder);
//v.im.setImageBitmap(result);
}
}
}.execute(pd.getId());
}*/
@Override
public int getItemCount() {
return imageAndNames.size();
}
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
public void SetOnItemClickListener(final OnItemClickListener itemClickListener) {
this.clickListener = itemClickListener;
}
}
答案 0 :(得分:0)
您可以在不使用xml文件的情况下以编程方式添加这些选项,如下所示,这可能会对您有帮助。
这里ONE和TWO显示了您在弹出菜单中给出的选项的索引(实际上是int值)。就像第一个位置编辑POST第二个位置REMOVE POST等。
1)在holder.cart上单击打开弹出菜单,如下所示:
private final static int ONE = 1;
private final static int TWO = 2;
PopupMenu popupMenu = new PopupMenu(this, findViewById(R.id.img_detail_information_options));
popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, getResources().getString(R.string.detail_information_edit_post));
popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, getResources().getString(R.string.detail_information_remove_post));
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
上面的代码会显示如下弹出窗口: - Screenshot