将用户输入与String Array列表进行比较

时间:2014-06-27 12:46:04

标签: java android string arraylist

所以我一直试图这样做几天,一直在搜索'尝试不同的东西,但它们似乎都不适合我。我不想将用户输入与String ArrayList进行比较,看看它是否具有该值。我这样做是为了防止用户发送他们想要的任何文本,只发送String ArrayList中的内容。这是简化的代码:

`

        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


             for (int i = 0; i < 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;
             }

`

就我而言。此代码似乎正在发送信息,但textview仍然会更改为警告。 你能帮我吗?非常感谢您的阅读。

4 个答案:

答案 0 :(得分:0)

使用.equals进行比较,因此请更改

if(searchNames.get(i).toString().toUpperCase()
                                          .contains(nameCaptureNo
                             .getText().toString().toUpperCase()) ) {

 if(searchNames.get(i).toString().toUpperCase()
                                      .equals(nameCaptureNo
                         .getText().toString().toUpperCase()) ) {

或直接使用.contains作为

无需循环搜索名称,因此请使用以下代码.contains,因此请更改

   for (int i = 0; i < searchNames.size(); i++) {
                 if            (searchNames.get(i).toString().toUpperCase()
                                      .contains(nameCaptureNo
                         .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;
             }

if(searchNames.contains(nameCaptureNo.getText().toString().toUpperCase()){
  send.setVisibility(View.VISIBLE);
}
else{
  send.setVisibility(View.INVISIBLE);
   //warning                   
    tvTi.setText("Izvēlies pilsētu no saraksta!");
   tvTi.setTextColor(Color.RED);
}

注意:在searchNames中保存uppercase个值。

答案 1 :(得分:0)

您可以从循环中执行所有更改。但是,只需要一个元素,这使得字符串的if条件true有效/发送。如果在某些先前的循环迭代中,else块被执行,则不应触发警告。将你的for循环更改为:

boolean found = false;
for (int i = 0; i < searchNames.size(); i++) {
    if (searchNames.get(i).toString().toUpperCase()
           .contains(nameCaptureNo
                       .getText().toString().toUpperCase())) {
        found = true;
        // save additional info you need to call send to variables here
        break;
    }
}
if (found) {
    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);
}

同样s1.contains(s2)检查,如果s2s1的子字符串,即s1="Hello World!"s2="World"将产生真。您可能希望使用equalsequalsIgnoreCase

答案 2 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

send.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          boolean isContain = false;
          for (int i = 0; i < searchNames.size(); i++) {
                if (searchNames.get(i).toString().toUpperCase().contains(nameCaptureNo.getText().toString().toUpperCase())) {
                     isContain = true;
                     break;
                 }
          }
          if(isContain){
              send.setVisibility(View.VISIBLE);
              tvTi.setText("");
              //here goes code wich sends info
          }else{
              send.setVisibility(View.INVISIBLE);
              tvTi.setText("Izvēlies pilsētu no saraksta!");
              tvTi.setTextColor(Color.RED);
          }
     }
});

答案 3 :(得分:0)

示例代码如下工作

您无需更改为大写。使用equalsIgnoreCase,如下所示,

public boolean checkString(String value)
{
boolean found = false;

for(String searchcontent:searchNames)
{
if((searchcontent).equalsIgnoreCase(value))
{
                found = true;
                break;
            }

        }


        return found;
}

enter image description here

完整代码如下,

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 the 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>