2

我创建了一个AutoCompleteTextView搜索课程标题列表(从 sqlite db 获得),我想要做的是,当用户从下拉菜单中单击标题时,数据库中关于他的全部信息选择出现在创建于下方的文本视图中AutoCompleteTextView

我对编程很陌生,尤其是对于 android,如果有人能在下面解释我如何准确地setOnItemClickListener调用数据库中的实例,我将不胜感激TextView

布局代码(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);
}
4

1 回答 1

3

首先,您应该尝试使用 aCursorAdapter而不是从中获取数组。检查此链接以获取更多信息。

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

如果你查看这个类的 src 代码,好消息是设置的变量setThreshold()只在这个方法中使用:

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

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

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

于 2010-12-12T13:44:26.357 回答