Android AutoCompleteTextView onClick问题

时间:2010-12-12 12:52:33

标签: android autocompletetextview

我创建了一个AutoCompleteTextView来搜索课程标题列表(从sqlite数据库中获取),我想要做的是,当用户点击下拉菜单中的标题时,数据库中有关其选择的全部信息显示在AutoCompleteTextView下面创建的文本视图中。

我对编程非常陌生,特别是对于android而言,如果有人能解释我如何使用setOnItemClickListener来调用下面TextView数据库中的实例,我真的很感激。< / p>

布局代码(R.layout.main_courses)是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:padding="5dp">
<AutoCompleteTextView 
 android:id="@+id/autocomplete_course"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search for a course"/>
<TextView
 android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/autocomplete_course"
    android:hint="Information about the course will appear here" />
</RelativeLayout>

我到目前为止写的AutoCompleteTextView的代码是:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main_courses);
    DataBase db = new DataBase(this.getApplicationContext());
 db.openDataBase();
 ArrayList<String> aCourses = db.getCoursesArr();
 db.close();


 AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.autocomplete_course);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_courses, aCourses);
 search.setAdapter(adapter);
}

1 个答案:

答案 0 :(得分:3)

首先,您应该尝试使用CursorAdapter而不是从中获取数组。查看this link了解详情。

AutoCompleteTextView中有一种方法可让您决定用户在显示下拉列表前必须输入多少个字母setThreshold。问题是它只允许&gt; = 1值。

如果您检查this class src code,好消息是setThreshold()设置的变量仅用于此方法:

public boolean enoughToFilter() {
  return getText().length() >= mThreshold;
}

所以我要尝试的第一件事是扩展AutoCompleteTextView并覆盖该方法以始终返回true。

注意:请注意,这可能会在将来发生变化,并且可能会被破坏。