android - RecyclerView - Remove Items and Leave Items Out -


i have two-part question hoping can assist with. reading data api. 1 of results of api ( in custom library made app) whether result has image or not.

i trying add functionality user can remove cards , not show them if not have image. able on load (they store selection) or on fly.

question one

is possible leave out items when populating recyclerview cardviews? can make image stay out doesn't issue of leaving card out.

question two

is possible remove multiple items recyclerview on fly? have tried for-loop cycle through of list items , if not have image remove them. issue works half time , when takes 4 clicks checkbox so.

note: below code inside of oncheckedchangedlistener

if(imageonly.ischecked()){     image = true;     for(int = 0; < strains.size(); i++){         if(strains.get(i) != null) {             if (strains.get(i).getimage().equalsignorecase(getcontext().getresources().getstring(r.string.no_image))) {                 strains.remove(i);                 adapter.notifyitemremoved(i);             }         }      }      adapter.notifydatasetchanged(); }else{    image = false; } 

adding adapter

public class straincardadapter extends recyclerview.adapter<recyclerview.viewholder> {     private final list<strain> strains;     private context context;     private boolean imageonly;      public straincardadapter(list<strain> strains, context context, boolean imageonly) {         this.strains = strains;         this.context = context;         this.imageonly = imageonly;     }      @override     public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) {         view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.strain_card_view, parent, false);         strainviewholder svh = new strainviewholder(v);         return svh;     }      @override     public void onbindviewholder(recyclerview.viewholder holder, int position) {         if (!strains.get(position).getimage().equals(context.getresources().getstring(r.string.no_image))) {             picasso.with(context).load(strains.get(position).getimage()).into(((strainviewholder) holder).strainimage);         }     }      @override     public int getitemcount() {         return strains.size();     }      public static class strainviewholder extends recyclerview.viewholder {         cardview straincard;         imageview strainimage;          public strainviewholder(view itemview) {             super(itemview);             straincard = (cardview) itemview.findviewbyid(r.id.strain_card);             strainimage = (imageview) itemview.findviewbyid(r.id.strain_image);         }     }  } 

you can, response objects api call you've made, can check if contain images or not. guessing have way determine it. assuming have imageurl associated it; indicate empty image may using empty string or null.

looping through objects , choosing ones have images should way go.

arraylist<strain> strainslist = new arraylist<>(); string noimage = getcontext().getresources().getstring(r.string.no_image) for( strain strain : responseobjects ) {   if( !strain.getimage().equalsignorecase(noimage) ) {       strainslist.add(strain);   } }   strains.addall( strainslist ); //assuming strains arraylist used adapter adapter.notifydatasetchanged(); 

removing items should straight forward :

you can add image indicating item can removed in adapter's item layout [possibly red x]; clicking on can remove item clicked.

public static class strainviewholder extends recyclerview.viewholder implements view.onclicklistener {         cardview straincard;         imageview strainimage;         imageview removeimageview;          public strainviewholder(view itemview) {             super(itemview);             straincard = (cardview) itemview.findviewbyid(r.id.strain_card);             strainimage = (imageview) itemview.findviewbyid(r.id.strain_image);             removeimageview = (imageview) itemview.findviewbyid(r.id.remove_image_view);             removeimageview.setonclicklistener(this);         }          @override         public void onclick(view v) {            if( v.getid() == r.id.remove_image_view ) {               int position = getadapterposition();                if( position != recyclerview.no_position ) {                  strains.remove(position);                  notifyitemremoved(position);               }            }         }     } 

let me know if more clarification needed.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -