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!