java - Search View with my custom adapter -
i trying make search view adapter:
public class notesadapter extends baseadapter implements filterable { /** * wrapper para notas. util para cambiar el fondo de los item seleccionados. */ paint paint = new paint(); public static final string key_title = "title"; public static final string key_body = "body"; public static final string key_catid = "id"; private static final string database_table = "notes_schema-v%s.sql"; private sqlitedatabase mdb; private arraylist<notesadapter.noteviewwrapper> notesdata; public context context; public void notifydatasetchanged() { super.notifydatasetchanged(); } @override public filter getfilter() { return null; } public static class noteviewwrapper { private final note note; private boolean isselected; /** * contruye un nuevo notewrapper con la nota dada. * * @param note una nota. */ public noteviewwrapper(note note) { this.note = note; } public note getnote() { return note; } public void setselected(boolean isselected) { this.isselected = isselected; } } private static final dateformat datetime_format = dateformat.getdatetimeinstance(dateformat.medium, dateformat.medium); private final list<noteviewwrapper> data; /** * constructor. * * @param data la lista de notas usar como fuente de datos para este adaptador. */ public notesadapter(list<noteviewwrapper> data) { this.data = data; } /** * @return cuantos datos hay en la lista de notas. */ @override public int getcount() { return data.size(); } /** * @param position la posición de la nota que se quiere * @return la nota en la posición dada. */ @override public noteviewwrapper getitem(int position) { return data.get(position); } /** * @param position una posición * @return la misma posición dada */ @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { // inflar componente visual convertview = layoutinflater.from(parent.getcontext()).inflate(r.layout.notes_row, parent, false); //cardview cardsview = (cardview) convertview.findviewbyid(r.id.note_cardboard); holder = new viewholder(convertview); convertview.settag(holder); } else holder = (viewholder) convertview.gettag(); // ya existe, solo es reciclarlo // inicializa la vista con los datos de la nota noteviewwrapper noteviewwrapper = data.get(position); holder.noteidtext.settext(string.valueof(noteviewwrapper.note.getid())); holder.notetitletext.settext(noteviewwrapper.note.gettitle()); // se la nota è più di 80 caratteri"..." holder.notecontenttext.settext(noteviewwrapper.note.getcontent().length() >= 80 ? noteviewwrapper.note.getcontent().substring(0, 80).concat("...") : noteviewwrapper.note.getcontent()); holder.notedatetext.settext(datetime_format.format(noteviewwrapper.note.getupdatedat())); // cambia il colore se la nota è cliccata if (noteviewwrapper.isselected) { cardview cardsview = (cardview) convertview.findviewbyid(r.id.note_cardboard); // sfondo grigio quando nota selezionata cardsview.setcardbackgroundcolor(color.parsecolor("#cacaca")); //cardsview.setcardbackgroundcolor(r.drawable.statelist_item_background); //cardsview.setbackgroundresource(r.drawable.selected_note); //holder.parent.setbackgroundcolor(parent.getcontext().getresources().getcolor(r.color.selected_note)); //linearlayout.layoutparams imagelayoutparams = new linearlayout.layoutparams( // linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent); } else { cardview cardsview = (cardview) convertview.findviewbyid(r.id.note_cardboard); // sfondo bianco quando nota non selezionata cardsview.setcardbackgroundcolor(color.white); //holder.parent.setbackgroundcolor(parent.getcontext().getresources().getcolor(android.r.color.transparent)); } return convertview; } /** * almacena componentes visuales para acceso rápido sin necesidad de buscarlos muy seguido. */ private static class viewholder { private textview noteidtext; private textview notetitletext; private textview notecontenttext; private textview notedatetext; private view parent; /** * constructor. encuentra todas los componentes visuales en el componente padre dado. * * @param parent un componente visual. */ private viewholder(view parent) { this.parent = parent; noteidtext = (textview) parent.findviewbyid(r.id.note_id); notetitletext = (textview) parent.findviewbyid(r.id.note_title); notecontenttext = (textview) parent.findviewbyid(r.id.note_content); notedatetext = (textview) parent.findviewbyid(r.id.note_date); } }}
i quite new , don't know how can it...so can not it. can me?
currently have:
searchview.setonquerytextlistener(new materialsearchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string query) { return false; } @override public boolean onquerytextchange(string newtext) { return false; } }); searchview.setonsearchviewlistener(new materialsearchview.searchviewlistener() { @override public void onsearchviewshown() { //do magic } @override public void onsearchviewclosed() { //do magic } });
more info: https://github.com/heromine/service-notes
please add public method filter(string searchtext) in adapter class;
private arraylist<notesadapter.noteviewwrapper> notesdata;
make copy of these arraylist
add item copied arraylist
public void filter(string charitem) { string searchitem = charitem.tolowercase(locale.getdefault); notesdata.remove(); if(searchitem.length()<0){ notesdata.addall(copylist); } else{ for(myitem item : copylist){ if(item.name.contines(searchitem){ notesdata.add(item); } } notifydatasetchanged(); }
call these function searchview onquerytextchaged(){} method , pass value.
Comments
Post a Comment