我正在使用3个AutocompleteTextViews来建议数据库中的条目。 我将AutocompleteTextView子类化,以便在单击时将默认文本设置为null,如果移走则设置回默认指令,并且不输入任何内容。
我使用SimpleCursorAdapter绑定到视图,但我发现我无法从OnItemClickListener获取AutocompleteTextView的id,我需要在变量中的所选行中添加其他信息,具体取决于它来自哪个AutocompleteTextView。我可以访问的只是AutoCompleteTextView $ DropDownListView,它是一个未记录的内部类,似乎没有提供真正的功能。也没有办法升级视图层次结构以获取原始的AutocompleteTextView。
因此,我将SimpleCursorAdapter子类化,并在构造函数中添加了一个int,以识别适配器来自哪个AutocompleteTextView,并且我能够从传递给OnItemClick()的视图中访问它。所以,虽然我的解决方案工作正常,但我想知道是否可以从DropDownListView获取AutocompleteTextView的id?
我还在使用另一个数据库查询,它从OnItemClick获取id,然后查找该项的数据,因为我找不到将多个列转换为字符串的方法。我应该使用CursorAdapter来保存启动另一个查询吗?哦,另外一件事,我最初需要一个数据库游标(all_cursor),当我正在做的就是过滤它以获得一个新游标?看起来像是矫枉过正。
活动 ....
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();
自动完成课程:
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);}
}
}
}
}
适配器:
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;
}
}
抱歉,这是很多代码!谢谢你的帮助。
斯蒂芬
答案 0 :(得分:1)
不是使用this
作为侦听器,而是创建一个新的侦听器类并为其提供自动完成的textview:
public class MyActivity extends Activity {
// .... somewhere
from_auto_complete.setOnItemClickListener(new MyClickListener(from_auto_complete));
private class MyClickListener implements OnClickListener {
AutoCompleteTextView autoComplete;
MyClickListener(AutoCompleteTextView actv) {
autoComplete = actv;
}
// ... handle clicks
}
}