我创建了一个 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);
}
有帮助吗?

解决方案

首先,您应该尝试使用 CursorAdapter 而不是从中获得数组。查看 这个链接 有关更多信息。

有一种方法 AutoCompleteTextView 让您确定用户在显示下拉之前必须输入多少个字母, SetThreshold. 。问题是它仅允许> = 1值。

如果您检查 此类SRC代码, ,好消息是变量设置为 setThreshold() 仅在此方法中使用:

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

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

笔记: 请记住,这可能在将来发生变化,并且可能会被打破。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top