vendredi 2 octobre 2015

Android AutoCompleteTextView: get item position

I'm creating a custom class in which I associate a Cursor (populated with values extracted from a SQLite database) to an AutoCompleteTextView, with an ArrayAdapter. Each record extracted from Cursor is represented by an ID and a value, and it's added to an ArrayAdapter by value. While adding values, I also create two ArrayList to keep track of both IDs and values.

I'd like to be able to get selected item position, but I actually cannot do it, even with onItemClick Listener.

Here it is some code from my custom class:

private AutoCompleteTextView field;
private String column;
private Activity activity;
private ArrayList<String> list_id, list_values;

//Constructor
public PopulateAutoComplete(int elementFromLayout, String column, Activity activity) {
    this.field = (AutoCompleteTextView) activity.findViewById(elementFromLayout);
    this.activity = activity;
    this.column = column;
}
//Reset two lists associated to actual element
private void initializeLists() {
    list_id= new ArrayList<>();
    list_values= new ArrayList<>();
}
//Populating methods
public void populate(Cursor cursor_total, String column_id, String column_values) {
    initializeLists();
    int i=0;
    String id = null;
    String value = null;
    cursor_total.moveToFirst();
    do {
        id = cursor_total.getString(cursor_total.getColumnIndex(column_id));
        value = cursor_total.getString(cursor_total.getColumnIndex(column_values));
        list_id.add(i,id);
        list_values.add(i,value);
        i++;
    } while (cursor_total.moveToNext());
    adapter = new ArrayAdapter<>(activity,android.R.layout.simple_dropdown_item_1line, list_values);
    field.setAdapter(adapter);
}
//Method which select the right item from the list
public void selectValue(Cursor cursor_single, String column_value_to_select) {
    String id_to_verify = cursor_single.getString(cursor_single.getColumnIndex(column_value_to_select);
    loop: {
        for (int i=0; i<listaID.size(); i++) {
            if (list_id.get(i).equals(id_to_verify)) {
                adapter.getItem(i);
                field.setText(list_values.get(i));
                break loop;
            }
        }
    }
    setListener();
}
private void setListener() {
    field.setOnItemClickListener(listener);
}
private AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //How to get ID of selected item here?
    }
};

And here it's the code from my MainActivity:

PopulateAutoComplete element = new PopulateAutoComplete(R.id.element, "column", this);
element.populate(cursor,"id","name");
element.selectValue(cursor_single,"id");

I'd like to have the ID of selected item, so I can use list_id.getItem(position).

I tried with field.getListSelection(), list_id.indexOf(adapterView.getSelectedItem()) but it was not helpful. I also know that some of this method are related to actual dropdown list, but I need a method which extract the exact position of an item in the ArrayAdapter; in this way, I can automatically extract ID and values (note: values are not unique).

Aucun commentaire:

Enregistrer un commentaire