Start an AutoCompleteTextView with a default object

5

I am working with a form to edit the information of a client, the form has three AutoCompleteTextView .

Each AutoCompleteTextView has an adapter that receives a List<Object> . As it is a form to edit, you need each AutoCompleteTextView to start with the Object that is saved in the database.

final AutoCompleteTextView acContactos = (AutoCompleteTextView) dialog.findViewById(R.id.acContactos);
final List<Contacto> contactos = new ContactoSQL(getContext()).getContactos();
final ArrayAdapter contactosAdapter = new ArrayAdapter<Contacto>(getContext(), android.R.layout.simple_dropdown_item_1line, contactos);
acContactos.setAdapter(contactosAdapter);
acContactos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       contacto = (Contacto) contactosAdapter.getItem(position);
   }
});

For example, the selected client has the contact with id 8, which in List<Object> has index 7 and that Contacto should be selected by default when opening the form to edit the Client.

I remain attentive to your comments and help, greetings.

    
asked by Marcelo 10.03.2016 в 00:49
source

1 answer

1

Your code is correct, you just have to add

acContactos.setThreshold(1);

so that when writing a character one of the names of the contacts is shown, that must be the reason why no name was shown (Considering that contactos contains the required objects).

    final AutoCompleteTextView acContactos = (AutoCompleteTextView) dialog.findViewById(R.id.acContactos);
    final List<Contacto> contactos = new ContactoSQL(getContext()).getContactos();
    final ArrayAdapter contactosAdapter = new ArrayAdapter<Contacto>(getContext(), android.R.layout.simple_dropdown_item_1line, contactos);
    acContactos.setAdapter(contactosAdapter);
    acContactos.setThreshold(1);  //* agregar!
    acContactos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

          contacto = (Contacto) contactosAdapter.getItem(position);

       }
    });
    
answered by 10.03.2016 в 06:58