从产生的AutoCompleteTextView $ DropDownListView获取有关AutocompleteTextView信息 [英] Get information about AutocompleteTextView from resulting AutoCompleteTextView$DropDownListView

查看:176
本文介绍了从产生的AutoCompleteTextView $ DropDownListView获取有关AutocompleteTextView信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用3 AutocompleteTextViews建议从数据库条目。 我子类AutocompleteTextView处理点击时设置默认的文本为null,并设置恢复到默认的指令,如果搬走了,并没有什么进入。

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.

我使用的是SimpleCursorAdapter结合的观点,但是我发现没有办法,我可以从OnItemClickListener,我需要把更多的信息,从所选择的行中的变量取决于获得AutocompleteTextView的ID这AutocompleteTextView它是从。所有我可以访问是AutoCompleteTextView $ DropDownListView,这是一个似乎没有提供真正的功能未公开的内部类。无论是有没有办法上去视图层次结构得到原始AutocompleteTextView。

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.

所以我子类SimpleCursorAdapter并添加一个int的构造,以确定哪些AutocompleteTextView适配器是从,我能够从()传递到OnItemClick视图访问此。所以,尽管我的解决办法正常工作,我不知道是否有可能从DropDownListView获得AutocompleteTextView的ID?

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?

我还使用另一个数据库查询,它会从该OnItemClick的ID,然后查找数据的该项目,是因为我找不到转换多个列字符串的方法。我应该使用的CursorAdapter为此,节省发起另一个查询?哦,还有一件事,我需要一个数据库游标最初(all_cursor)时,我做的是过滤就可以得到一个新的光标?似乎有点小题大做。

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.

活动 ......

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

适配器:

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

抱歉,这是一个很大的code!感谢您的帮助。

sorry that's a lot of code! Thanks for your help.

斯蒂芬

推荐答案

而不是使用作为听者,创建一个新的监听器类,并给它你自动完成的TextView:

Instead of using this as the listener, create a new listener class and give it your autocomplete 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
   }
}

这篇关于从产生的AutoCompleteTextView $ DropDownListView获取有关AutocompleteTextView信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆