Hello, I'm very new to programming, I'm really enthusiastic, I'm just looking for a type that
editext
looks for the letter or word in the text so that, for example, if my
string
is written "Mr. Jose Ventura "and I type only" Ventura "it can be located. Thanks in advance, I am grateful if you post the code.
public class MainActivity extends ActionBarActivity {
private ListView lv;
private EditText et;
private String[] lst;
private ArrayList<String> lst_Encontrados = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvlist);
et = (EditText) findViewById(R.id.etlist);
lst = new String[] {"Sr. José Ventura","Sra. Maria Cícera","Vô Antonio"};
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst));
CarregarEncontrados();
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
CarregarEncontrados();
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lst_Encontrados));
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
AlertDialog.Builder test = new AlertDialog.Builder(MainActivity.this);
if(((TextView) view).getText().equals("Sr. José Ventura")){
test.setTitle("test1");
test.setMessage("0123456789");
test.setNeutralButton("OK", null);
test.show();
}
if(((TextView) view).getText().equals("Sra. Maria Cícera")){
test.setTitle("Sra. Maria Cícera");
test.setMessage("486597231");
test.setNeutralButton("OK", null);
test.show();
}
if(((TextView) view).getText().equals("Vô Antonio")){
test.setTitle("Vô Antonio");
test.setMessage("23456892");
test.setNeutralButton("OK", null);
test.show();;;
}
}
});
}
public void CarregarEncontrados() {
int textlength = et.getText().length();
lst_Encontrados.clear();
for (int i = 0; i < lst.length; i++) {
if (textlength <= lst[i].length()) {
String textoAux = (String) lst[i].subSequence(0, textlength);
String textoFormatado = et.getText().toString();
textoAux = removeAcentos(textoAux);
textoFormatado = removeAcentos(textoFormatado);
if (textoFormatado.equalsIgnoreCase(textoAux)) {
lst_Encontrados.add(lst[i]);
}
}
}
}
private static Map<Character, Character> acentosMap;
public static String removeAcentos(String texto) {
if (acentosMap == null || acentosMap.size() == 0) {
acentosMap = new HashMap<>();
acentosMap.put('À', 'A');
acentosMap.put('Á', 'A');
acentosMap.put('Â', 'A');
acentosMap.put('Ã', 'A');
acentosMap.put('Ä', 'A');
acentosMap.put('È', 'E');
acentosMap.put('É', 'E');
acentosMap.put('Ê', 'E');
acentosMap.put('Ë', 'E');
acentosMap.put('Í', 'I');
acentosMap.put('Ì', 'I');
acentosMap.put('Î', 'I');
acentosMap.put('Ï', 'I');
acentosMap.put('Ù', 'U');
acentosMap.put('Ú', 'U');
acentosMap.put('Û', 'U');
acentosMap.put('Ü', 'U');
acentosMap.put('Ò', 'O');
acentosMap.put('Ó', 'O');
acentosMap.put('Ô', 'O');
acentosMap.put('Õ', 'O');
acentosMap.put('Ö', 'O');
acentosMap.put('Ñ', 'N');
acentosMap.put('Ç', 'C');
acentosMap.put('ª', 'A');
acentosMap.put('º', 'O');
acentosMap.put('§', 'S');
acentosMap.put('³', '3');
acentosMap.put('²', '2');
acentosMap.put('¹', '1');
acentosMap.put('à', 'a');
acentosMap.put('á', 'a');
acentosMap.put('â', 'a');
acentosMap.put('ã', 'a');
acentosMap.put('ä', 'a');
acentosMap.put('è', 'e');
acentosMap.put('é', 'e');
acentosMap.put('ê', 'e');
acentosMap.put('ë', 'e');
acentosMap.put('í', 'i');
acentosMap.put('ì', 'i');
acentosMap.put('î', 'i');
acentosMap.put('ï', 'i');
acentosMap.put('ù', 'u');
acentosMap.put('ú', 'u');
acentosMap.put('û', 'u');
acentosMap.put('ü', 'u');
acentosMap.put('ò', 'o');
acentosMap.put('ó', 'o');
acentosMap.put('ô', 'o');
acentosMap.put('õ', 'o');
acentosMap.put('ö', 'o');
acentosMap.put('ñ', 'n');
acentosMap.put('ç', 'c');
acentosMap.put('ç', 'c');
}
if (texto == null) {
return "";
}
StringBuilder sb = new StringBuilder(texto);
for (int i = 0; i < texto.length(); i++) {
Character c = acentosMap.get(sb.charAt(i));
if (c != null) {
sb.setCharAt(i, c);
}
}
return sb.toString();
}