Android:将字符串输入(edittext)与arraylist进行比较

时间:2014-01-17 15:40:05

标签: java android arrays arraylist

我想将输入字符串与另一个类的数组列表进行比较,因此我使用searchfield来比较它们。如下所示它工作正常。

输入类

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);
            }
        });}

}

比较课程

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;         
         }          

 }
}

列出类

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

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

    return word;
    }

}

但是在添加第二个时:

列出类

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

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

    return word;
    }

}

'列'应用程序停止。

任何人都知道如何搜索第二个条目?

1 个答案:

答案 0 :(得分:0)

对于对该问题的解决方案感兴趣的每个人,必须将for循环更改为:

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

在arraylist中添加一些条目:

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

    return word;
    }

现在工作正常。