android - Filtering a listview based on fields not displayed in listview -


i have app grabs json file internet, parses through , creates , item object it. items have following fields: type (image, text), , data (this url if item type image or string of text if item type text)

when app first loads list displays mix of items both types text , images. if item image, image displayed in list item in listview, if it's text text displayed.

what need do(and having problem with) need when user selects menu "image only" show objects have type "image" in listview , hide items have type "text", if select "text only", filters out image list items , displays items of type "text". if select "all" should display default when app first loads.

i not display object's type field on listview anywhere, data field of object either image loaded url or text. seems every filtering example come across when types text filters list text displayed in list , visible in list need filter not visible on list..i'm not quite sure how accomplish need. have ideas? explained enough

edit: have far. now, when select images menu works, when select text menu works, when toggle it's blank , mitems.size 0 though never removed it.

on create:

 protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     tvnoinet=(textview)findviewbyid(android.r.id.empty);     setsupportactionbar(toolbar);     if (isnetworkavailable()) {         getdata thejsondata = new getdata();         thejsondata.execute();     }      mylistview = (listview) findviewbyid(r.id.listviewid);     customadapter = new listadapter(mainactivity.this, r.layout.list,mitems);     mylistview.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             if (mitems.get(position).getmtype().tostring().equals("text")){                 if (isnetworkavailable()) {                     intent in = new intent(mainactivity.this, mywebview.class);                     startactivity(in);                 }                 else{                     toast.maketext(getapplicationcontext(), "no internet connection, try again later", toast.length_short).show();                 }              }             if (mitems.get(position).getmtype().tostring().equals("image")){                 intent in = new intent(mainactivity.this,fullimage.class);                 in.putextra("imageurl",mitems.get(position).getmdata().tostring());                 startactivity(in);             }         }     });   } 

my options menu select 1 of options (all, images, text)

@override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_about) {         intent intent = new intent(this, about.class);         startactivity(intent);         return true;     }      if (id == r.id.all) {         item.setchecked(true);         customadapter.clear();         customadapter.addall(mitems);         return true;      }     if (id == r.id.images) {         item.setchecked(true);         customadapter.clear();         customadapter.addall(mimageitems);         return true;      }     if (id == r.id.text) {         item.setchecked(true);         customadapter.clear();         customadapter.addall(mtextitems);         return true;     }      return super.onoptionsitemselected(item); } 

my task grabs json, parses it, creates object , adds mitems, if type image adds mimageitems , if type text adds mtextitems.

public class getdata extends asynctask<string, void, string> {     string jsonstr = null;     progressdialog progress = new progressdialog(mainactivity.this);     @override     protected string doinbackground(string... params) {         httpurlconnection urlconnection = null;         bufferedreader reader = null;          try {             url url = new url("http://host/data.json");             urlconnection = (httpurlconnection) url.openconnection();             urlconnection.setrequestmethod("get");             urlconnection.connect();             inputstream inputstream = urlconnection.getinputstream();             stringbuffer buffer = new stringbuffer();             if (inputstream == null) {                 return null;             }             reader = new bufferedreader(new inputstreamreader(inputstream));              string line;             while ((line = reader.readline()) != null) {                 buffer.append(line + "\n");             }              if (buffer.length() == 0) {                 return null;             }             jsonstr = buffer.tostring();         } catch (ioexception e) {             log.e("main", "error ", e);             return null;         } finally{             if (urlconnection != null) {                 urlconnection.disconnect();             }             if (reader != null) {                 try {                     reader.close();                 } catch (final ioexception e) {                     log.e("placeholderfragment", "error closing stream", e);                 }             }         }         try {              jsonarray itemsarray = new jsonarray(jsonstr);              string itemid=null;             string itemtype=null;             string itemdate=null;             string itemdata=null;             (int = 0; < itemsarray.length(); i++) {                 jsonobject jsonitem=itemsarray.getjsonobject(i);                 if (jsonitem.has("id")){                     itemid=jsonitem.getstring("id");                 }                 if (jsonitem.has("type")){                     itemtype=jsonitem.getstring("type");                 }                 if (jsonitem.has("date")){                     itemdate=jsonitem.getstring("date");                 }                 if (jsonitem.has("data")){                     itemdata=jsonitem.getstring("data");                 }                 item myitem=new item(itemid,itemtype,itemdate,itemdata);                 mitems.add(myitem);                 if (itemtype.equals("image")){                     mimageitems.add(myitem);                 }                 else if (itemtype.equals("text")){                     mtextitems.add(myitem);                 }                 }             log.e("all size: ", string.valueof(mitems.size()));             log.e("text size: ", string.valueof(mtextitems.size()));             log.e("image size: ", string.valueof(mimageitems.size()));         } catch (jsonexception jsone) {             jsone.printstacktrace();             log.e(log_tag, "error processing json data");         } 

and adapter:

class listadapter extends arrayadapter<item>  {    private list<item> items;      public listadapter(context context, int textviewresourceid) {         super(context, textviewresourceid);      }      public listadapter(context context, int resource, list<item> items) {          super(context, resource, items);          this.items = items;      }      @override     public view getview(int position, view convertview, viewgroup parent) {          view v = null;         textview tn = null;         imageview img = null;          if (convertview == null) {             layoutinflater vi;             vi = layoutinflater.from(getcontext());             v = vi.inflate(r.layout.list, null);         }         else {             v=convertview;         }          item p = items.get(position);         tn = (textview) v.findviewbyid(r.id.tvtext);         img = (imageview) v.findviewbyid(r.id.thumbnail);             if (p.getmtype().equals("image")) {                 img.setvisibility(view.visible);                 picasso.with(getcontext()).load(p.getmdata()).error((r.drawable.placeholder_error)).placeholder(r.drawable.placeholder).resize(90,0).into(img);                 tn.settext("id: " + p.getmid()+"\ntype: " + p.getmtype() +"\ndate: " + p.getmdate()+ "\ndata: " +  p.getmdata());             } else {                 img.setvisibility(view.invisible);                 tn.settext("id: " + p.getmid()+"\ntype: " + p.getmtype() +"\ndate: " + p.getmdate()+ "\ndata: " +  p.getmdata());             }         return v;     } } 

now when app loads mixed list showing items, if select "images" menu image types showing in list, if select "text" text items showing , can toggle , forth between image , text fine. however, when select "all" again list blank , mitems.size 0. i'm on head one. haha way found around when i'm adding item object list, have 4th list called mallitems gets set same mitems mitems.add(myitem); next line mallitems.add(myitem)and in menu selection set mallitems. know i'm doing wrong have learn somehow right?

i think you're close.

you have 3 lists. since adapter model listview, make more sense have lists in adapter.

public class listadapter extends baseadapter {      public enum listmode {         images_and_text,         images_only,         text_only     }      private listmode mlistmode = listmode.images_and_text;      private list<item> mitems;      private list<item> mimages;      private list<item> mtexts;      @override     public int getcount() {         switch (mlistmode) {             case images_and_text:                 return mitems == null ? 0 : mitems.size();             case images_only:                 return mimages == null ? 0 : mimages.size();             case text_only:                 return mtexts == null ? 0 : mtexts.size();         }         return 0;     }      @override     public object getitem(int position) {         switch (mlistmode) {             case images_and_text:                 return mitems == null ? null : mitems.get(position);             case images_only:                 return mimages == null ? null : mimages.get(position);             case text_only:                 return mtexts == null ? null : mtexts.get(position);         }         return null;     }      @override     public long getitemid(int position) {         return position;    // not used     }      @override     public view getview(int position, view convertview, viewgroup parent) {          view v = null;         textview tn = null;         imageview img = null;          if (convertview == null) {             layoutinflater vi;             vi = layoutinflater.from(getcontext());             v = vi.inflate(r.layout.list, null);         } else {             v=convertview;         }          item p = items.get(position);         tn = (textview) v.findviewbyid(r.id.tvtext);         img = (imageview) v.findviewbyid(r.id.thumbnail);         if (p.getmtype().equals("image")) {             img.setvisibility(view.visible);             picasso.with(getcontext()).load(p.getmdata()).error((r.drawable.placeholder_error)).placeholder(r.drawable.placeholder).resize(90,0).into(img);             tn.settext("id: " + p.getmid()+"\ntype: " + p.getmtype() +"\ndate: " + p.getmdate()+ "\ndata: " +  p.getmdata());         } else {             img.setvisibility(view.invisible);             tn.settext("id: " + p.getmid()+"\ntype: " + p.getmtype() +"\ndate: " + p.getmdate()+ "\ndata: " +  p.getmdata());         }         return v;     }      /**      * call when filter selected, e.g.      * <code>madapter.setlistmode(listmode.images_and_text);</code>      * @param listmode      */     public void setlistmode(listmode listmode) {         mlistmode = listmode;         notifydatasetchanged();     }      /**      * after receive json, call method add items adapter.      * @param items      */     public void setitems(jsonarray jsonarray) {          mitems = new arraylist<>();         mimages = new arraylist<>();         mtexts = new arraylist<>();         (int = 0; < jsonarray.length(); i++) {             item item = new item(jsonarray.get(i));             mitems.add(item);             if (item.isimage()) {                 mimages.add(item);             }             if (item.istext()) {                 mtexts.add(item);             }         }          notifydatasetchanged();     }  } 

instantiate adapter this:

    .     .     .     mylistview = (listview) findviewbyid(r.id.listviewid);     customadapter = new listadapter();     mylistview.setadapter(customadapter);     .     .     . 

when asynctask runs, set items on adapter:

        .         .         .         jsonarray itemsarray = new jsonarray(jsonstr);         customadapter.setitems(itemsarray);         .         .         . 

change view menu this:

    @override     public boolean onoptionsitemselected(menuitem item) {          switch (item.getitemid()) {         case r.id.action_about:             intent intent = new intent(this, about.class);             startactivity(intent);             return true;          case r.id.all:             item.setchecked(true);             customadapter.setlistmode(listmode.images_and_text);             return true;          case r.id.images:             item.setchecked(true);             customadapter.setlistmode(listmode.images_only);             return true;          case r.id.text:             item.setchecked(true);             customadapter.setlistmode(listmode.text_only);             return true;         }          return super.onoptionsitemselected(item);     } 

also, add constructor item class takes jsonobject:

    public item(jsonobject jsonitem) {          string itemid=null;         string itemtype=null;         string itemdate=null;         string itemdata=null;          if (jsonitem.has("id")) {             itemid=jsonitem.getstring("id");         }         if (jsonitem.has("type")) {             itemtype=jsonitem.getstring("type");         }         if (jsonitem.has("date")){             itemdate=jsonitem.getstring("date");         }         if (jsonitem.has("data")){             itemdata=jsonitem.getstring("data");         }         this(itemid,itemtype,itemdate,itemdata);     } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -