2

I am new to using recyclerview and adapters. I have the RecyclerView and an Onclick working (thanks to Google: https://github.com/googlesamples/android-RecyclerView). It does have an setOnClickListener which writes to the Log. But I'm trying to figure out how to remove an item from the list when it is clicked - I can't figure out how to get to the ArrayList which the adapter is using and run notifyItemInserted to update the display.

In reviewing another post (Android RecyclerView addition & removal of items - Android RecyclerView addition & removal of items) it looks like paradite/Jared Burrows were able to get it running. But some of the code appears to be missing and I was hoping that someone could fill in the holes for me. (I couldn't comment on this other post to ask the question there).

For example, I don't see that mItemClickListener is defined anywhere. I'm also not sure of the connection between the class which extends the RecyclerView.Adapter and the class which extends the RecyclerView.ViewHolder.

Here is my RecyclerView.Adapter:

public class RVAdapter1 extends RecyclerView.Adapter<RVAdapter1.ViewHolder> {
private static final String TAG = "debug";
private int itemCounter = 0;

private List<String> feedItemList;

public static class ViewHolder extends RecyclerView.ViewHolder {
    private final TextView textView;

    public ViewHolder(View v) {
        super(v);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Element " + getPosition() + " clicked.");
                //feedItemList.remove(getPosition());
               //notifyItemRemoved(getPosition());
            }
        });
        textView = (TextView) v.findViewById(R.id.textView);
    }

    public TextView getTextView() {
        return textView;
    }
}

public RVAdapter1(List<String> dataSet) {
    feedItemList = dataSet;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.text_row_item, viewGroup, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    Log.d(TAG, "Element " + position + " set.");
    viewHolder.getTextView().setText(String.valueOf(feedItemList.get(position)));
}

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

public void addItem(int position, String data) {
    feedItemList.add(position, data);
    notifyItemInserted(position);
    Log.d(TAG,"adapter list length=" + feedItemList.size());
}
}    

I am trying to use the adapter from the other StackOverflow question, but I am unsure of how to connect (or combine?) the two adapters and where the mItemClickListener comes from).

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public CardView mCardView;
public TextView mTextViewTitle;
public TextView mTextViewContent;
public ImageView mImageViewContentPic;
//......
public ImageView imgViewRemoveIcon;
public ViewHolder(View v) {
    super(v);
    mCardView = (CardView) v.findViewById(R.id.card_view);
    mTextViewTitle = (TextView) v.findViewById(R.id.item_title);
    mTextViewContent = (TextView) v.findViewById(R.id.item_content);
    mImageViewContentPic = (ImageView) v.findViewById(R.id.item_content_pic);
    //......
    imgViewRemoveIcon = (ImageView) v.findViewById(R.id.remove_icon);

    mTextViewContent.setOnClickListener(this);
    imgViewRemoveIcon.setOnClickListener(this);
    v.setOnClickListener(this);
    mTextViewContent.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(view, getPosition());
            }
            return false;
        }
    });
}

@Override
public void onClick(View v) {
    //Log.d("View: ", v.toString());
    //Toast.makeText(v.getContext(), mTextViewTitle.getText() + " position = " + getPosition(), Toast.LENGTH_SHORT).show();
    if(v.equals(imgViewRemoveIcon)){
        removeAt(getPosition());
    }else if (mItemClickListener != null) {
        mItemClickListener.onItemClick(v, getPosition());
        }
    }
}

public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
}

public void removeAt(int position) {
    mDataset.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(getPosition, mDataSet.size());
}    

This may be a trivial question but I certainly would appreciate any help that anyone is willing to offer... Thanks!

Community
  • 1
  • 1
dbaddorf
  • 35
  • 1
  • 5

2 Answers2

1

From what I can understand you're having trouble knowing which item has been clicked? So you need the getPosition() method.

My advice would be to use the setTag() method of views, where the tag will be the position of the feed item in your list. It should look something like this:

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    Log.d(TAG, "Element " + position + " set.");
    viewHolder.getTextView()
         .setText(String.valueOf(feedItemList.get(position)));
    viewHolder.getRemoveIcon().setTag(position);
}

After this create an onClickListener like this:

@Override
public void onClick(View v) {
    int position = (Integer)v.getTag();
    feedItemList.remove(position);
    notifyItemRemoved(position);
}

For this to work you can make the adapter extend the View.OnClickListener, that way you have access to the array and to the notify methods.

Gent Ahmeti
  • 1,559
  • 13
  • 17
  • Thanks so much for your reply! My problem wasn't that I couldn't tell which item was clicked but that I couldn't access the ArrayList to remove the item from the code where the OnClick was. See my answer below. – dbaddorf Jan 17 '15 at 23:46
-1

Ok, I got it to work. Instead of having the ViewHolder be a "public static class" I needed to make it just "public class". Then I was able to modify my arraylist directly from the setOnClickListener block:

    public class ViewHolder extends RecyclerView.ViewHolder {
    private final TextView textView;

    public ViewHolder(View v) {
        super(v);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Element " + getPosition() + " clicked.");
                feedItemList.remove(getPosition());
                notifyItemRemoved(getPosition());
            }
        });
        textView = (TextView) v.findViewById(R.id.textView);
    }

    public TextView getTextView() {
        return textView;
    }
}
dbaddorf
  • 35
  • 1
  • 5