Android AutoCompleteTextView CursorAdapter using SQLite Database and ArrayAdapter

AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with. The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key. The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold.

The following code example shows how to create a AutoCompleteTextView which suggests various items with the help of CursorAdapter using SQLite Database and countries names using String ArrayAdapter while the user is still typing...


Android AutoCompleteTextView CursorAdapter and ArrayAdapter

Android AutoCompleteTextView CursorAdapter and ArrayAdapter

Android AutoCompleteTextView CursorAdapter and ArrayAdapter

Source code for AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.as400samplecode" android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="13" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application android:icon="@drawable/icon" android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light">
        >
        <activity android:name=".AndroidAutocompleteTextViewActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Source code for main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal">
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_height="match_parent" android:orientation="vertical"
        android:layout_width="match_parent">
        <LinearLayout android:id="@+id/linearLayout6"
            android:layout_height="wrap_content" android:orientation="horizontal"
            android:paddingBottom="10dp" android:layout_gravity="right"
            android:layout_width="match_parent" android:paddingTop="50dp">
            <TextView android:text="Simple AutoCompleteTextView using String Array"
                android:textStyle="bold" android:layout_height="wrap_content"
                android:id="@+id/textView6" android:textSize="25sp"
                android:layout_width="wrap_content" android:layout_weight="1" />
        </LinearLayout>
        <LinearLayout android:id="@+id/itemLayout2"
            android:layout_width="match_parent" android:orientation="vertical"
            android:layout_height="wrap_content">
            <TableLayout android:layout_width="wrap_content"
                android:id="@+id/tableLayout1" android:layout_height="match_parent"
                android:stretchColumns="1">
                <TableRow android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:id="@+id/tableRow4">
                    <AutoCompleteTextView android:layout_height="wrap_content"
                        android:layout_width="fill_parent" android:hint="Search for Country in AutoCompleteTextView"
                        android:id="@+id/autocomplete_country">
                        <requestFocus />
                    </AutoCompleteTextView>
                </TableRow>
            </TableLayout>
        </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout5"
            android:layout_height="wrap_content" android:orientation="horizontal"
            android:paddingBottom="10dp" android:layout_gravity="right"
            android:layout_width="match_parent" android:paddingTop="50dp">
            <TextView android:text="AutoCompleteTextView using SQLite DbAdapter"
                android:textStyle="bold" android:layout_height="wrap_content"
                android:id="@+id/textView6" android:textSize="25sp"
                android:layout_width="wrap_content" android:layout_weight="1" />
        </LinearLayout>
        <LinearLayout android:id="@+id/itemLayout"
            android:layout_width="match_parent" android:orientation="vertical"
            android:layout_height="wrap_content">
            <TableLayout android:layout_width="wrap_content"
                android:id="@+id/tableLayout1" android:layout_height="match_parent"
                android:stretchColumns="1">
                <TableRow android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:id="@+id/tableRow4">
                    <TextView android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:text="" android:id="@+id/textView1" />
                    <AutoCompleteTextView android:layout_height="wrap_content"
                        android:layout_width="fill_parent" android:hint="Search Item"
                        android:id="@+id/autocomplete_desc">
                        <requestFocus />
                    </AutoCompleteTextView>
                </TableRow>
                <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView android:textSize="25sp" android:text="Item Number"
                        android:id="@+id/text1" android:layout_height="wrap_content"
                        android:paddingRight="20sp" android:layout_width="250dp" />
                    <EditText android:layout_height="wrap_content"
                        android:layout_width="wrap_content" android:id="@+id/item" />
                </TableRow>
                <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView android:textSize="25sp" android:layout_height="wrap_content"
                        android:paddingRight="20sp" android:layout_width="wrap_content"
                        android:id="@+id/text4" android:text="Description" />
                    <TextView android:text="" android:id="@+id/itemDesc"
                        android:layout_width="wrap_content" android:layout_height="wrap_content" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Source code for country_list.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#000">
</TextView>

Source code for item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/MyListItem"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:paddingTop="5dp" android:paddingBottom="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <TextView android:id="@+id/text1" style="?android:attr/dropDownItemStyle"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:singleLine="true" android:layout_height="40sp"
        android:layout_width="match_parent" />
    <TextView android:id="@+id/text2" style="?android:attr/dropDownItemStyle"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:singleLine="true" android:layout_width="fill_parent"
        android:layout_height="40sp" />
</LinearLayout>

Click here for next Chapter



No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.