java - Comparing user input to String Array list -
so i've been trying couple of days, been searchin' , trying different stuff, none of them seem work me. wan't compare user input string arraylist , see if has value. prevent users send in whatever text want, in string arraylist. here's simplified code:
`
private arraylist<string> searchnames = new arraylist<string>(); edittext namecaptureno; edittext namecaptureuz; button send; textview tvti; public void sendtestmsg() { // todo auto-generated method stub send.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub (int = 0; < searchnames.size(); i++) { if (searchnames.get(i).tostring().touppercase() .contains(namecaptureno .gettext().tostring().touppercase()) && searchnames.get(i).tostring().touppercase() .contains(namecaptureuz .gettext().tostring().touppercase()) ) { send.setvisibility(view.visible); //here goes code wich sends info }else{ send.setvisibility(view.invisible); //warning tvti.settext("izvēlies pilsētu no saraksta!"); tvti.settextcolor(color.red); continue; } break; }
`
this far i've gotten. code seems sending info, textview still changes warning. can help? many reading.
sample code working below
you don't need change upper case. use equalsignorecase below,
public boolean checkstring(string value) { boolean found = false; for(string searchcontent:searchnames) { if((searchcontent).equalsignorecase(value)) { found = true; break; } } return found; }
complete code below,
mainactivity.java
package com.example.userinput; import java.util.arraylist; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.text.editable; import android.text.textwatcher; import android.util.log; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { private arraylist<string> searchnames = new arraylist<string>(); edittext namecaptureno; edittext namecaptureuz; button send; textview tvti; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); namecaptureno = (edittext) findviewbyid(r.id.namecaptureno); namecaptureuz = (edittext) findviewbyid(r.id.namecaptureuz); namecaptureno.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { log.d("mainactivity", "text modified "+ namecaptureno.gettext().tostring()); } @override public void ontextchanged(charsequence s, int start, int before, int count) { log.d("mainactivity", "text modified "+ namecaptureno.gettext().tostring()); if(checkstring(namecaptureno.gettext().tostring())) { send.setvisibility(view.visible); } else { send.setvisibility(view.invisible); //warning tvti.settext("izvēlies pilsētu no saraksta!"); tvti.settextcolor(color.red); } } @override public void aftertextchanged(editable s) { // todo auto-generated method stub log.d("mainactivity", "text modified "+ namecaptureno.gettext().tostring()); } }); send = (button) findviewbyid(r.id.send); //set send button invisible send.setvisibility(view.invisible); tvti = (textview) findviewbyid(r.id.tvti); searchnames.add("test1"); searchnames.add("test2"); searchnames.add("test3"); searchnames.add("test4"); searchnames.add("test5"); searchnames.add("test6"); } public boolean checkstring(string value) { boolean found = false; for(string searchcontent:searchnames) { if((searchcontent).equalsignorecase(value)) { found = true; break; } } return found; } }
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packagename}.${activityclass}" > <textview android:id="@+id/tvti" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textsize="30dp" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/tvti" android:layout_margintop="36dp" android:text="namecaptureno" android:textappearance="?android:attr/textappearancesmall" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview1" android:layout_margintop="32dp" android:text="namecaptureuz" android:textappearance="?android:attr/textappearancesmall" /> <edittext android:id="@+id/namecaptureuz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textview3" android:layout_alignbottom="@+id/textview3" android:layout_alignleft="@+id/namecaptureno" android:layout_marginleft="16dp" android:ems="10" /> <button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/namecaptureuz" android:layout_marginleft="40dp" android:layout_margintop="30dp" android:text="send" /> <edittext android:id="@+id/namecaptureno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textview1" android:layout_alignbottom="@+id/textview1" android:layout_alignparentright="true" android:ems="10" /> </relativelayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">userinput</string> <string name="hello_world">textview tvti</string> </resources>
Comments
Post a Comment