0

When I search with my autocompletetextview and I first write a space or two spaces, no word of the String appears. How can I do to search with my autocompletetextview independent if I write a space first?

The function .trim() serves me? or .replaceAll("\\s+$", "") serves me? How can I implement this in my code? This is my code:

package com.example.epson.buscadormep;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final String[] values = new String[]{"Cilindros pequeños de oxigeno gaseoso o de aire, de uso medicinal",
                "Dispositivos que contienen oxigeno liquido",

        };
        //final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,values);
       final ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,R.layout.custom_autocomplete_item,R.id.autoCompleteItem,values);
       final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.txtMercancias);


        autoCompleteTextView.setThreshold(1);//will start working from second character
        autoCompleteTextView.setAdapter(adapter); //setting the adapter data into the AutoCompleteTextView

        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // LinearLayout ll = (LinearLayout) view;
                //This will give you the string value of selected list item
                //TextView listItem = (TextView) ll.getChildAt(0);
                //You can do this or apply own logic to find the selected value case

                if (autoCompleteTextView.getText().toString().equals("Cilindros pequeños de oxigeno gaseoso o de aire, de uso medicinal"))
                {
                    startActivity(new Intent( MainActivity.this, Main2Activity.class));
                }
                if (autoCompleteTextView.getText().toString().equals("Dispositivos que contienen oxigeno liquido"))
                {
                    startActivity(new Intent( MainActivity.this, Main3Activity.class));
                }

                }


            }
        });


        button=(Button)findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                autoCompleteTextView.setText("");
            }
        });

    }

}
ASutherland
  • 77
  • 1
  • 8

2 Answers2

0
.replaceAll("\\s","")

Removes all whitespace characters from a string.

You can place it after your .toString() method.

It should be noted that this removes All whitespace from the string so you will have remove all whitespace from the comparison.

If you want to remove whitespace from the begining and ending of the string use .trim(). Like the regex, put it after your .toString() method.

SWG
  • 36
  • 3
0

String's class trim() method is sufficient for your query. Here's how you can do it:

final String text = "  foo   ";
System.out.println(text.trim());

// in addition to above code, below code of line only replaces all leading whitespaces with empty strings, but you should above trim() method so your search can continue even after trailing whitespaces
System.out.println(text.replaceFirst("\\s++$", ""));
Prashant Saini
  • 3,539
  • 1
  • 10
  • 24
Sach
  • 69
  • 2