在recyclerview项上,单击在对话框中显示自定义recyclerview数据

时间:2018-10-11 15:57:57

标签: android android-recyclerview onitemclick

我的活动有recyclerview。当用户单击recyclerview项目时,我想显示我的自定义对话框并在对话框中显示recyclerview项目数据,recylerview正常运行,但是问题是当我单击该项目时,应用程序不幸由于某些错误而停止了运行

这是我的日志

  

进程:com.example.ajsoft.test,PID:5290       java.lang.ArrayIndexOutOfBoundsException:length = 18;指数= -1           在java.util.ArrayList.get(ArrayList.java:310)           在com.example.ajsoft.test.RecyclerViewAdapter $ 1.onClick(RecyclerViewAdapter.java:50)           在android.view.View.performClick(View.java:4780)           在android.view.View $ PerformClick.run(View.java:19866)           在android.os.Handler.handleCallback(Handler.java:739)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5254)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

RecyclerViewAdapter代码

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

private Context mContext ;
private List<Book> mData ;
Dialog myDialog;

public RecyclerViewAdapter(Context mContext, List<Book> mData) {
    this.mContext = mContext;
    this.mData = mData;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view ;
    LayoutInflater mInflater = LayoutInflater.from(mContext);
    view = mInflater.inflate(R.layout.my,parent,false);

    final MyViewHolder vHolder =new MyViewHolder(view);

  myDialog =new Dialog(mContext);
  myDialog.setContentView(R.layout.vx);

  vHolder.cardView.setOnClickListener(new View.OnClickListener() {
      @Override 
      public void onClick(View v) { 
          TextView dialog_name=(TextView)myDialog.findViewById(R.id.name);
          ImageView dialog_image=(ImageView)myDialog.findViewById(R.id.image);
          dialog_name.setText(mData.get(vHolder.getAdapterPosition()).getTitle());
          dialog_image.setImageResource(mData.get(vHolder.getAdapterPosition()).getThumbnail());
          myDialog.show(); }
          });
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {

    holder.tv_book_title.setText(mData.get(position).getTitle());
    holder.img_book_thumbnail.setImageResource(mData.get(position).getThumbnail());

}

@Override
public int getItemCount() {
    return mData.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView tv_book_title;
    ImageView img_book_thumbnail;
    CardView cardView ;

    public MyViewHolder(View itemView) {
        super(itemView);

        tv_book_title = (TextView) itemView.findViewById(R.id.name) ;
        img_book_thumbnail = (ImageView) itemView.findViewById(R.id.image);
        cardView = (CardView) itemView.findViewById(R.id.cv);


    }
}


}

3 个答案:

答案 0 :(得分:2)

问题出在您的onCreateViewHolder()方法的这一部分:

final MyViewHolder vHolder =new MyViewHolder(view);
...
vHolder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override 
    public void onClick(View v) { 
        ...
    }
});
return new MyViewHolder(view);

您返回的MyViewHolder实例与创建OnClickListener时使用的实例不同。这意味着侦听器中的vHolder变量引用的是从未添加到RecyclerView的ViewHolder,因此其位置将始终为-1

您可以通过将return语句更改为使用原始ViewHolder来解决此问题:

return vHolder;

答案 1 :(得分:0)

在您的代码中添加以下更改:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view ;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.my,parent,false);

myDialog =new Dialog(mContext);
myDialog.setContentView(R.layout.vx);

return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {

holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getThumbnail());
holder.cardView.setOnClickListener(new View.OnClickListener() {
  @Override 
  public void onClick(View v) { 
      TextView dialog_name=(TextView)myDialog.findViewById(R.id.name);
      ImageView dialog_image=(ImageView)myDialog.findViewById(R.id.image);
      dialog_name.setText(mData.get(vHolder.getAdapterPosition()).getTitle());
      dialog_image.setImageResource(mData.get(vHolder.getAdapterPosition()).getThumbnail());
      myDialog.show(); }
      });
}

希望它将对您有帮助。.

答案 2 :(得分:0)

ArrayIndexOutOfBoundsException 当您尝试访问超出范围的数组索引时(因此名称),将导致此异常。

@Ben在上面提供了索引超出范围的原因。我认为他的解决方案应该在这里起作用。