java - Is there a more efficient way to update the listview or loop through a string array -


i trying write simple word search program android. supposed match each letter searched user , display matches local dictionary.

everything working smaller dictionary when try use larger 1 program collapses. guess search function loops through each word inefficient.

following code project.

public class mainactivity extends actionbaractivity {     string[] items;     int length;     arraylist<string> listitems;     arrayadapter<string> adapter;     listview listview;     edittext edittext;      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         listview=(listview)findviewbyid(r.id.listview);         edittext=(edittext)findviewbyid(r.id.txtsearch);         items = readfile().split("\\n");         initlist();            edittext.addtextchangedlistener(new textwatcher() {              @override             public void beforetextchanged(charsequence s, int start, int count, int after) {                 length=s.tostring().length();             }              @override             public void ontextchanged(charsequence s, int start, int before, int count) {                 if (s.tostring().equals("")) {                     initlist();                 }                 else {                     searchitem(s.tostring());                 }             }              @override             public void aftertextchanged(editable s) {                 if (s.tostring().length() < length) {                     initlist();                     (string item:items) {                         if (!item.tolowercase().contains(s.tostring().tolowercase())) {                             listitems.remove(item);                         }                     }                 }             }         });     }      public string readfile(){          inputstream input = getresources().openrawresource(getresources().getidentifier("words","raw", getpackagename()));         bufferedreader br = null;         stringbuilder sb = new stringbuilder();          string finalstring = "";          try {             string line;             br = new bufferedreader(new inputstreamreader(input));              while ((line = br.readline()) != null) {                // finalstring += line + "\n";                 sb.append(line + "\n");             }          } catch (ioexception e) {             e.printstacktrace();         } {             if (br != null) {                 try {                     br.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         }          return sb.tostring();      }      public void searchitem (string searchword) {         for(string item:items){             if(!item.contains(searchword)){                 listitems.remove(item);             }         }         adapter.notifydatasetchanged();     }   public void initlist(){          listitems=new arraylist<>(arrays.aslist(items));         adapter=new arrayadapter<string>(this, r.layout.list_item, r.id.txtitem, listitems);         listview.setadapter(adapter);     }  @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_settings) {         return true;     }      return super.onoptionsitemselected(item); } 

}

as mentioned, believe searchitem(string searchword) problem maybe there wrong data update of adapter?

note: have used breakpoints , crashes when loop within searchitem(string searchword) called upon.

many guys!

i changed edittext , used searchview instead. fixed problem!


Comments