2010-03-30 31 views
6

我使用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; 
} 
    } 

对不起,这是一个很大的代码!谢谢你的帮助。

斯蒂芬

回答

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

了一段时间我回顾这一点,但它确实工作。我将OnClickListener更改为OnItemClickListener,以提供DropDown中的所有记录详细信息。非常感谢,我知道必须有一个答案! – 2012-04-15 23:18:06