简体   繁体   中英

Android: compare string input (edittext) with arraylist

I want to compare my input string with an array list of another class, so I used the searchfield to compare them. As below it works fine.

Input Class

public class Suche extends Activity{

private Button zumergebnis;
final Intent zum_ergebnis = new Intent("android.intent.action.activity_ergebnis");

static String mystring;

static void setstring(String textView){
    mystring = textView;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_suche);



    zumergebnis = (Button) findViewById(R.id.zumergebnis);

        zumergebnis.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){

            EditText searchinput = (EditText) findViewById(R.id.editText1);
             String from = searchinput.getText().toString(); 

             setstring(from);

             startActivity(zum_ergebnis);
            }
        });}

}

Comparing Class

public class Ergebnis extends Suche
{

private TextView textView;

static String getstring(){
    return mystring;
}

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ergebnis);


    textView = (TextView) findViewById(R.id.textView2);
    textView.setText(mystring);

    class2 Object = new class2();
    final ArrayList<String> word = Object.method2();   

    ListAdapter listenAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, word);

    ListView mystringsView = (ListView) findViewById(R.id.listView1);
    mystringsView.setAdapter(listenAdapter);

            textView = (TextView) findViewById(R.id.textView2);
            String searchField = (mystring);
         for (String s : word) 
         {
               if (searchField.toString().contains(s)) 
                       {
                            textView.setText("Your word"+mystring+"exists in the list");
                        }
                       else
                       {
                        textView.setText("Word"+mystring+"doesnt exist");
                        continue;
                       }
                    break;         
         }          

 }
}

list class

    public ArrayList method2(){ 
    ArrayList<String> word = new ArrayList<String>();

    word.add("uno");
    word.add("dos");
    word.add("tres");

    return word;
    }

}

but when adding a second:

list class

    public ArrayList method2(){ 
    ArrayList<String> word = new ArrayList<String>();

    word.add("uno; one");
    word.add("dos; two");
    word.add("tres; three");

    return word;
    }

}

'column' the app stops.

Anyone an idea how to even search for the second entry?

For everyone who is interested in the solution for that problem, the for- loop has to be changed to:

 for (int i = 0; i < word.size(); i++) {
                if (word.get(i).contains(searchField.trim())) {
                    textView.setText("Your input '" +mystring+ "' exists.");
                }
                else

adding some entries to the arraylist:

    public ArrayList method2(){ 
    ArrayList<String> word = new ArrayList<String>();
    word.add("uno; one");
    word.add("dos; two");

    return word;
    }

it works fine now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM