从生成的AutoCompleteTextView$DropDownListView中获取有关AutocompleteTextView的信息

6
我正在使用3个AutocompleteTextView来从数据库中建议输入内容。我创建了AutocompleteTextView的子类来处理在点击时将默认文本设置为空,并在移开焦点且未输入任何内容时重新设置为默认说明。
我原本使用SimpleCursorAdapter来绑定视图,但是我发现无法从OnItemClickListener中获取AutocompleteTextView的id,而我需要根据它来将选定行的其他信息放入变量中。我只能访问AutoCompleteTextView$DropDownListView,这是一个未记录的内部类,似乎没有实际功能。也没有办法通过查找视图层次结构来获取原始的AutocompleteTextView。
因此,我创建了SimpleCursorAdapter的子类,并添加了一个int参数来标识适配器来自哪个AutocompleteTextView。我可以从传递给OnItemClick()的视图中访问这个参数。因此,虽然我的解决方案很好用,但我想知道是否可能从其DropDownListView获取AutocompleteTextView的id?
我还使用另一个数据库查询,在OnItemClick中获取id,然后查找该项的数据,因为我找不到将多列转换为字符串的方法。我应该使用CursorAdapter来保存另一个查询吗?还有一件事,当我只是在它上过滤以获取新的cursor时,是否需要初始数据库cursor(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);}
}
}
}
}

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;
}
    }

抱歉,这是很多代码!感谢您的帮助。
史蒂芬
1个回答

2

不要使用this作为监听器,创建一个新的监听器类并将其赋给你的自动完成文本框:

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
   }
}

1
我花了一些时间来审查这个,但它确实有效。我将OnClickListener更改为OnItemClickListener,以便从DropDown中提供所有记录详细信息。非常感谢,我知道一定有答案! - Stev_k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接