Search code examples
androidautocompletetextview

Android AutoCompleteTextView return wrong string


I have a little problem in my android app. I want to put a word in an AutoCompleteTextView and then jump to a specific Activity(depend on the word that the user gave).

The problem is that, when i give a specific word, the program does not correspond with the correct answer as expected but returns a wrong toast message. However with the log that i put i see the correct answer. I think that the solution is silly but i stuck and need to solve this.

the code:

Activity.java

public class paralActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_paral);
    final String [] temp = {"one","two","three"};
    AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
    actv.clearListSelection();

    final RelativeLayout myRelative = (RelativeLayout) findViewById(R.id.find);
    myRelative.setVisibility(View.INVISIBLE);

    ImageView myImage = (ImageView) findViewById(R.id.aktoploika);
    myImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(paralActivity.this, topParalActivity.class);
            startActivity(intent);
        }
    });

    ImageView myOtherImage = (ImageView) findViewById(R.id.aeroporika);
    myOtherImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myRelative.setVisibility(View.VISIBLE);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(paralActivity.this, android.R.layout.select_dialog_item, temp);
            //Getting the instance of AutoCompleteTextView
             AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
            actv.setThreshold(1);//will start working from first character
            actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
            actv.setTextColor(Color.RED);
            ImageView findBeach = (ImageView) findViewById(R.id.find_beach);
            findBeach.setOnClickListener(new View.OnClickListener() {
                @Override
             **   public void onClick(View view) {
                    AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
                    String choice = actv.getText().toString();


                    Log.i("Answer",choice);
                    if (choice == "one"){
                        Intent firstIntent = new Intent(paralActivity.this, nauagioActivity.class);
                        startActivity(firstIntent);
                    }else if (choice == temp[1]){
                        Intent secondIntent = new Intent(paralActivity.this, gerakasActivity.class);
                        startActivity(secondIntent);
                    }else if (choice == temp[2]){
                        Intent thirdIntent = new Intent(paralActivity.this, limnionasActivity.class);
                        startActivity(thirdIntent);
                    }else{
                        Toast.makeText(paralActivity.this,"wrong",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    });

}
}

To help u understand, in this activity a have 2 imageViews. When the user presses the second, a relativeLayout appears (with an AutoCompleteTextView and a button inside). After the user writes the word when presses the button it must go to the specific activity. I declared a String Array (temp[3]) with three words inside and 3 activities for each of the words.

The problem starts in the last onclick method that i put ** . Every time i put a correct word from the Array i take the Toast message but in the log i see the correct.

here is Activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_paral"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main"
android:scaleX="2"
tools:context="com.example.billy.zakynthosapp.paralActivity">




<TextView
    android:id="@+id/categories"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/welcome"
    android:text="@string/categories"
    android:textAlignment="center"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:textSize="11sp"
    android:textStyle="bold"
    android:textColor="@color/welcome"
    android:textAllCaps="true"/>



<ImageView
    android:id="@+id/aktoploika"
    android:layout_centerHorizontal="true"
    android:layout_width="130dp"
    android:layout_height="50dp"
    android:layout_below="@id/categories"
    android:layout_marginTop="35dp"
    android:scaleType="centerCrop"
    android:src="@drawable/par1" />


<TextView
    android:id="@+id/aktoploika_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/aktoploika"
    android:layout_alignTop="@+id/aktoploika"
    android:layout_alignRight="@+id/aktoploika"
    android:layout_alignBottom="@+id/aktoploika"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="@string/paralies"
    android:textSize="22sp"
    android:textColor="@color/categories"
    android:textStyle="bold"/>


<ImageView
    android:id="@+id/aeroporika"
    android:layout_centerHorizontal="true"
    android:layout_width="130dp"
    android:layout_height="50dp"
    android:layout_below="@id/aktoploika"
    android:layout_marginTop="35dp"
    android:scaleType="centerCrop"
    android:src="@drawable/par2" />



<TextView
    android:id="@+id/aeroporika_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/aeroporika"
    android:layout_alignTop="@+id/aeroporika"
    android:layout_alignRight="@+id/aeroporika"
    android:layout_alignBottom="@+id/aeroporika"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="@string/search"
    android:textSize="22sp"
    android:textColor="@color/categories"
    android:textStyle="bold" />


<RelativeLayout
    android:id="@+id/find"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/aeroporika"
    android:layout_marginTop="50dp"
    android:layout_centerInParent="true"
    android:background="@color/categories"
    android:scaleX="0.5">


    <TextView
        android:id="@+id/textView_1"
        android:layout_width="wrap_content"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:layout_centerHorizontal="true"
        android:text="@string/find_paral"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/find_beach"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="200dp"
        android:src="@drawable/find"
        android:onClick="find"/>

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView_1"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:text="">

        <requestFocus />
    </AutoCompleteTextView>

</RelativeLayout>

Can anyone help me with this? Thanks a lot!


Solution

  • Try choice.equals("one") instead of choice == "one"