简体   繁体   English

Android:比较字符串输入(edittext)和arraylist

[英]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循环都必须更改为:

 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: 向arraylist添加一些条目:

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

    return word;
    }

it works fine now. 现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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