Question

I've created an AutoCompleteTextView to search through a list of course titles (obtained from an sqlite db) and what I want to do is, when the user clicks on a title from the drop-down menu, the whole information from the database about his selection appears in a text view created below the AutoCompleteTextView.

I am pretty new to programming, especially for android and I would really appreciate it if someone could explain me how exactly to use setOnItemClickListener to call an instance in the database in the TextView below.

The code for the layout (R.layout.main_courses) is

<?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>

and the code for the AutoCompleteTextView I've written so far is:

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);
}
Was it helpful?

Solution

First of all you should try using a CursorAdapter instead of getting an array from it. Check this link for more info.

There is a method in AutoCompleteTextView that let you decide how many letters the user must type before the dropdown is shown, setThreshold. The issue is that it only allows >=1 values.

If you check this class src code, the good news is that the variable set by setThreshold() is only used in this method:

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

So the first thing I would try is extending AutoCompleteTextView and override that method to always return true.

NOTE: Keep in mind that this might change in the future and it can get broken.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top