Yasmine Guemouria
Yasmine Guemouria

Reputation: 27

ImageViewHolder.getAdapterPosition() / .getLayoutPosition() does not work

I am using a RecyclerView to display a bunch of images that I've uploaded to firebase. When I click on an image, a popupMenu appears with some buttons to add and delete that image.

I want to get my recyclerView's actual position to then parse that position into a Firebase query to add that image to a different "table" or delete it. This is what my onBindViewHolder looks like in my Adapter class :

    @Override
    public void onBindViewHolder(final ImageViewHolder holder, int position) {
        Upload uploadCurrent = mUploads.get(position);
        holder.textViewName.setText(uploadCurrent.getName());
        Picasso.with(mContext)
            .load(uploadCurrent.getImageUrl())
            .placeholder(R.mipmap.ic_launcher)
            .fit()
            .centerCrop()
            .into(holder.imageView);

        pos = holder.getAdapterPosition(); 
        uploadAtPosition = mUploads.get(pos);
        Log.e(Integer.toString(pos),"error is :");
}

and this is the code I use in my Activity :

    public void showPopupMenu(View view) {

        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.setOnMenuItemClickListener(this);
        MenuInflater inflater = popupMenu.getMenuInflater();
        inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
        popupMenu.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.addToMenu :
                String uploadId = menuDatabaseRef.push().getKey();
          menuDatabaseRef.child(uploadId).setValue(ImageAdapter.uploadAtPosition);

            case R.id.deleteFromMenu :

            case R.id.deleteFromDatabase :
        }

        return false;
}     

The problem I have is that .getAdapterPosition() does not return the real current item position in the RecylerView. Sometimes it skips a number, and when I'm scrolling back up it doesn't go like 6,5,4...,0.

I must be doing something wrong, please help.

I have also tried .getLayoutPosition() and .getItemId()...

Upvotes: 0

Views: 333

Answers (2)

Manoj Perumarath
Manoj Perumarath

Reputation: 10234

Try to use the position variable you already have with you

@Override
public void onBindViewHolder(final ImageViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    holder.textViewName.setText(uploadCurrent.getName());
    Picasso.with(mContext)
        .load(uploadCurrent.getImageUrl())
        .placeholder(R.mipmap.ic_launcher)
        .fit()
        .centerCrop()
        .into(holder.imageView);

    pos = position; 
    uploadAtPosition = mUploads.get(pos);
    Log.e(Integer.toString(pos),"error is :");
}

Upvotes: 0

Sai
Sai

Reputation: 288

You have to use setTag() and getTag() in bindview holder,so you will able to get exact position of particular item.

For Example:

    holder.textViewName.setTag(position);
    holder.textViewName.setOnClickListener(View v)
    {
    int pos=v.getTag();
    uploadAtPosition = mUploads.get(pos);
    }

Upvotes: 1

Related Questions