How to Delete or Remove Cardview from Recyclerview - Android Studio

How to delete or remove CardView from RecyclerView? Android Studio

Solution:

Inside your onBindView(..) write like this:

holder.deleteExercise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
exercises.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
notifyItemRangeChanged(holder.getAdapterPosition(), exercises.size());
}
});

Hope it Helps.

How to delete a cardview from recyclerView and sqlite database? (Kotlin - Android)

There are a few possible strategies, here are two:

  • You either need to reload the list from the DB, tracking diffs, or
  • You need to manage the stored items list (e..g. bills) by removing items from it as you delete from the DB.

The way you have your adapter set up is that the initializing caller owns the items list. It's not usually a good idea for the adapter to modify a list passed to it, you need to either copy to a mutable collection, or reload.

You also need to call notifyItemRemoved, notifyItemRangeRemoved or notifyDataSetChanged (depending on how you implement removal) on the adapter after making your removal changes.

You may also want to look into DiffUtil.

How to remove Card View from Recycler View, from a separate Activity

In your PendingMusicianInfoAdapter you are using:-

rejectionButton = view.findViewById(R.id.musician_rejection); //here is your null pointer

Reason:-

In you card_musician_info.xml
you don't have any Button "musician_rejection"

NOTE:- Every time you face any null pointer issue it is most commonly due to wrong initialization/ id mismatch / or no reference in correct XML.
In this case, you may have "musician_rejection" earlier which was registered in your "R" class but later you may have removed it but in your "R" class it somehow stays. Or it can be quite possible that you may have "musician_rejection" in some other XML. Android Studio is not that smart enough to understand that use parent XML file for all its id references in JAVA file.

Use http://jakewharton.github.io/butterknife/ which eliminates all such issues.

Use debugger step by step, For better understanding.

Removing a cardview item inside a recycler view if it is null

check two things for this,

first check your limage is null or not, if it is null than hide your image view.

one another thing is check image is already exists on your server url. if not exists on server hide your image view.

you can try below code for that,

if(current.getLImage() == null || !imageExists(**yourImageUrl**)){
Picasso.with(this.context).cancelRequest(holder.image);
holder.image.setVisibility(View.GONE);
}


public static boolean imageExists(String URLName){
HttpClient client= new DefaultHttpClient();
HttpHead headMethod = new HttpHead(urlToImage);
HttpResponse response = client.execute(headMethod);
if(response.getStatusLine().getStatusCode== HttpStatus.SC_NOT_FOUND) {
return false;
} else {
return true;
}
}

How to add and remove CardView from LinearLayout on button click in android studio?

The parameter v in onDelete(View v) is not the CardView that you want to be removed.
It's the ImageView that you click.

In onAddField() do this:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());

what the above code does is store the new CardView object in delRowBtn's tag.
Also I removed -1 from parentRelativeLayout.getChildCount() to add the new CardView at the end.

But the above logic must be applied also to the 1st CardView that is already there, so in onCreate() add this:

CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);


In onDelete() do this:

CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);

what the above code does is retrieves the CardView object from delRowBtn's tag and removes it from parentRelativeLayout.

The full code of your Activity:

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ExperienceInfoActivity extends AppCompatActivity {
Toolbar toolbar;
private LinearLayout parentRelativeLayout;
private ViewGroup.LayoutParams params;
private int count = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_experience_info);

CardView experienceInfoActivityFormCardVw = findViewById(R.id.experienceInfoActivityFormCardVw);
ImageView delRowBtn = findViewById(R.id.delRowBtn);
delRowBtn.setTag(experienceInfoActivityFormCardVw);
params = experienceInfoActivityFormCardVw.getLayoutParams();

initViews();
initListeners();
}

private void initListeners() {

}

private void initViews() {
toolbar=(Toolbar) findViewById(R.id.toolbarExperienceInfoActivity);
toolbar.setTitle("Employee Experience Info");
toolbar.setTitleTextColor(getResources().getColor(R.color.toolBarTitle));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
setSupportActionBar(toolbar);

parentRelativeLayout = (LinearLayout) findViewById(R.id.experienceDetailsInfoRelLayout);
}

public void onAddField(View view) {
try{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.experience_details_row, null);
ImageView delRowBtn = rowView.findViewById(R.id.delRowBtn);
delRowBtn.setTag(rowView);
rowView.setLayoutParams(params);
parentRelativeLayout.addView(rowView, parentRelativeLayout.getChildCount());
EditText employerNameEditText = rowView.findViewById(R.id.employerNameEditText);
employerNameEditText.requestFocus();
count++;
}catch (Exception e){
e.printStackTrace();
}
}
public void onDelete(View v) {
try{
if (count == 1) return;
CardView cv = (CardView) ((ImageView) v).getTag();
parentRelativeLayout.removeView(cv);
count--;

}catch (Exception e){
e.printStackTrace();
}
}
}

Android RecyclerView addition & removal of items

I have done something similar.
In your MyAdapter:

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(position, mDataSet.size());
}

Edit:

getPosition() is deprecated now, use getAdapterPosition() instead.

Checking checkbox to delete RecylerView row deletes other row

You are missing if(isChecked) in OnCheckedChangeListener
You don't need to call notifyDataSetChanged(), use getAdapterPosition() instead of position.

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

spinner = itemView.findViewById(R.id.spinner);
editText = itemView.findViewById(R.id.editText);
textView5 = itemView.findViewById(R.id.textView5);
checkBox = itemView.findViewById(R.id.checkBox);
rootView = itemView.findViewById(R.id.linearLayout);

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// makes the set disappear when checkbox is ticked.
if(isChecked){

productList.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());



Toast.makeText(create, "Done!", Toast.LENGTH_LONG).show();
}

}
});

}

Add your code inside ProductViewHolder constructor

You need to create new product every time a new product added So:

 floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProduct= new Product() // **something like that which has some values, the thing is you need to create new product with new keyword**
productList.add(mProduct);
if(adapter != null)
adapter.notifyDataSetChanged();
//Handle the empty adapter here

}
});

the thing is you need to create new product with new keyword



Related Topics



Leave a reply



Submit