Get information about AutocompleteTextView from resulting AutoCompleteTextView$DropDownListView
- by Stev_k
I'm using 3 AutocompleteTextViews to suggest entries from a database.
I subclassed AutocompleteTextView to handle setting the default text to null when clicked and setting back to the default instructions if moved away and nothing is entered.
I was using a SimpleCursorAdapter to bind to the view, but I discovered that there was no way I could get the id of the AutocompleteTextView from an OnItemClickListener, which I needed to put additional information from the selected row in a variable depending on which AutocompleteTextView it was from. All I could access was the AutoCompleteTextView$DropDownListView, which is an undocumented inner class that appears to offer no real functionality. Neither was there a way to go up the view hierarchy to get the original AutocompleteTextView.
So I subclassed SimpleCursorAdapter and added an int to the constructor to identify which AutocompleteTextView the adapter was from, and I was able to access this from the view passed into OnItemClick(). So, although my solution works fine, I wonder if it is possible to get the id of an AutocompleteTextView from its DropDownListView?
I am also using another database query which gets the id from the OnItemClick and then looks up the data for that item, because I couldn't find a way of converting more than one column to a string. Should I be using CursorAdapter for this, to save initiating another query? Oh, and another thing, do I need a database cursor initially (all_cursor) when all I'm doing is filtering on it to get a new cursor? Seems like overkill.
Activity
....
dbse.openDataBase();
Cursor all_Cursor = dbse.autocomplete_query();
startManagingCursor(all_Cursor);
String[] from_all = new String[]{DbAdapter.KEY_NAME};
int[] to_all = new int[] {android.R.id.text1};
from_adapt = new AutocompleteAdapter(FROM_DBADAPTER, this,android.R.layout.simple_dropdown_item_1line, all_Cursor, from_all, to_all);
from_adapt.setStringConversionColumn(1);
from_adapt.setFilterQueryProvider(this);
to_adapt = new AutocompleteAdapter(TO_DBADAPTER, this,android.R.layout.simple_dropdown_item_1line, all_Cursor, from_all, to_all);
to_adapt.setStringConversionColumn(1);
to_adapt.setFilterQueryProvider(this);
from_auto_complete = (Autocomplete) findViewById(R.id.entry_from);
from_auto_complete.setAdapter(from_adapt);
from_auto_complete.setOnItemClickListener(this);
to_auto_complete = (Autocomplete) findViewById(R.id.entry_to);
to_auto_complete.setAdapter(to_adapt);
to_auto_complete.setOnItemClickListener(this);
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
Cursor selected_row_cursor = dbse.data_from_id(id);
selected_row_cursor.moveToFirst();
String lat = selected_row_cursor.getString(1);
String lon = selected_row_cursor.getString(2);
int source = ((AutocompleteAdapter) parent.getAdapter()).getSource();
Autocomplete class:
public class Autocomplete extends AutoCompleteTextView implements OnTouchListener,OnFocusChangeListener{
String textcontent;
Context mycontext = null;
int viewid = this.getId();
public Autocomplete(Context context, AttributeSet attrs) {
super(context, attrs);
textcontent = this.getText().toString();
mycontext = context;
this.setOnFocusChangeListener(this);
this.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
if (textcontent.equals(mycontext.getString(R.string.from_textbox)) |
textcontent.equals(mycontext.getString(R.string.to_textbox)) |
textcontent.equals(mycontext.getString(R.string.via_textbox))) {
this.setText("");
}
return false;
}
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
int a = this.getText().length();
if (a == 0){
if (viewid == R.id.entry_from) {this.setText(R.string.from_textbox);}
if (viewid == R.id.entry_to) {this.setText(R.string.to_textbox);}
if (viewid == R.id.entry_via) {this.setText(R.string.via_textbox);}
}
}
}
}
Adapter:
public class AutocompleteAdapter extends SimpleCursorAdapter {
int source;
public AutocompleteAdapter(int query_source, Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
source = query_source;
}
public int getSource() {
return source;
}
}
sorry that's a lot of code! Thanks for your help.
Stephen