Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove an item in RecyclerView- Android

I am facing a problem, I could not remove an Item in RecyclerView. I am trying to search in Internet, but could not see any solution I am following to this post Android RecyclerView addition & removal of items But I still could not remove the item selected Below is my code for my adapter and fragement

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
List<VideoDetails> contents2;
Context mContext;
ImageLoader imageLoader;
static final int TYPE_HEADER = 0;
static final int TYPE_CELL = 1;
public RecyclerViewAdapter(List<VideoDetails> contents) {
    this.contents2 = contents;
}
public RecyclerViewAdapter(Context context, List<VideoDetails> contents) {
    mContext = context;
    this.contents2 = contents;
    imageLoader = new ImageLoader(context);
}
@Override
public int getItemViewType(int position) {
    return position;

}
@Override
public int getItemCount() {
    return contents2.size();
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    View view = null;
    view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_card_small2, parent, false);
    ViewHolder vh = new ViewHolder(view);
    return vh;
}
 @Override
public void onBindViewHolder(ViewHolder holder,final int position) {
    holder.title.setText(contents2.get(position).getTitle());
     holder.image.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View view) {
             Log.d("thien.van","on Click onBindViewHolder");
             contents2.remove(position);
             notifyItemRemoved(position);
             notifyItemRangeChanged(position, contents2.size());

         }
     });
}
public Bitmap getThumbnail(String str) {

    Bitmap bMap = ThumbnailUtils.createVideoThumbnail(str, MediaStore.Video.Thumbnails.MINI_KIND);
    return bMap;
}
public class ViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener {


    public TextView title;
    public ImageView image;
    public ProgressBar progressBar;

    public ViewHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.title);
        image = (ImageView) itemView.findViewById(R.id.thumbnail);
        progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
      //  image.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        Log.d("thien.van","onClick getAdapterPosition             () = "+getAdapterPosition());
        removeAt(getAdapterPosition());
    }
}
public void removeAt(int position) {
    contents2.remove(position);
    notifyDataSetChanged();
}

}

RecyclerViewFragment

public class RecyclerViewFragment extends Fragment {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;

private static final int ITEM_COUNT = 2;

private List<Object> mContentItems = new ArrayList<>();
private static List<VideoDetails> mContentItems2 = new ArrayList<>();

public static RecyclerViewFragment newInstance() {
    return new RecyclerViewFragment();
}

public static RecyclerViewFragment newInstance(List<VideoDetails> contentItems) {
    RecyclerViewFragment myFragment = new RecyclerViewFragment();
    //  mContentItems = contentItems;
    Bundle args = new Bundle();
    args.putSerializable("thienvan", (Serializable) contentItems);
    myFragment.setArguments(args);
    return myFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mContentItems2 = (List<VideoDetails>) getArguments().getSerializable("thienvan");
    // Log.d("thien.van","Fragment file name = "+mContentItems2.get(0).getTitle());
    return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(layoutManager);
    //mRecyclerView.setHasFixedSize(true);

    mAdapter = new RecyclerViewMaterialAdapter(new RecyclerViewAdapter(getActivity().getBaseContext(),mContentItems2));
    mRecyclerView.setAdapter(mAdapter);

    /*{
        for (int i = 0; i < ITEM_COUNT; ++i)
            mContentItems.add(new Object());
        mAdapter.notifyDataSetChanged();
    }*/

    MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}
//http://android--code.blogspot.com/2015/12/android-recyclerview-add-remove-item.html

}

I could not remove an item inside Adapter class. Anybody could help me ?

like image 548
user3157841 Avatar asked Apr 26 '16 07:04

user3157841


1 Answers

To get the correct position for remove you should call holder.getAdapterPosition() and use this value for all remove and notify operations. Android studio shows a warning for this, so make sure to check the warnings first. And why for getItemCount you return 2?

public void onClick(View view) {
     int newPosition = holder.getAdapterPosition();
     Log.d("thien.van","on Click onBindViewHolder");
     contents2.remove(newPosition);
     notifyItemRemoved(newPosition);
     notifyItemRangeChanged(newPosition, contents2.size());

}
like image 87
Iulian Popescu Avatar answered Sep 18 '22 18:09

Iulian Popescu