Question

I want to load AutoCompleteTextView using text entered in same...e.g. suppose i have Employee table of my sqlite database with following rows... id empname dept 01 John IT 02 Joseph Account 03 Flintoff Account 04 Jacob IT

If I enter "J" in AutoCompleteTextView it should show "John" and "Joseph" in this. or If I enter "n" in AutoCompleteTextView it should show "John" and "Flintoff" in this.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expense);
    dpResult=(DatePicker)findViewById(R.id.dpResult);
    txtTime=(EditText)findViewById(R.id.edit_message);
    actvAcc=(AutoCompleteTextView)findViewById(R.id.actvAcc);
    txtSubAcc=(EditText)findViewById(R.id.txtExpense);
    txtAmount=(EditText)findViewById(R.id.txtAmount);
    dbHelper=new DatabaseHelper(this);

    Cursor c=dbHelper.getAllExpAccount();

// Cursor c=dbHelper.GetAccByChar(actvAcc.getEditableText().toString()); startManagingCursor(c);

    //SimpleCursorAdapter ca=new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, c, new String [] {DatabaseHelper.colDeptName}, new int []{android.R.id.text1});
    SimpleCursorAdapter ca=new SimpleCursorAdapter(this,R.layout.deptspinnerrow, c, new String [] {DatabaseHelper.colAccount,"_id"}, new int []{R.id.ACTAcount});
    //ca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    actvAcc.setThreshold(1);
    int desiredColumn = 3;
     ca.setCursorToStringConverter(null);
     ca.setStringConversionColumn(desiredColumn);
        actvAcc.setAdapter(ca);
}

is anything wrong in it..

Regards, Neilesh

Was it helpful?

Solution 3

  1. you must have unique .xml file in layout for your AutoTextCompleteView
  2. you must have data in string type in your adapter

OTHER TIPS

Use android:completionThreshold="1" inside AutoCompleteTextView.

With the help of this Tag if you will enter single character, will give you the desired result. All the matches output.

<AutoCompleteTextView
            android:id="@+id/autocomplete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:completionThreshold="1" />

And set your Adapter which contains all the information of Employee.

Hope this will help you.

Java code:

 AutoCompleteTextView acTextView;   
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_dropdown_item_1line, employee);
                acTextView = (AutoCompleteTextView) findViewById(R.id.autotext);
                acTextView.setThreshold(1);
                acTextView.setAdapter(adapter);
                acTextView.setTextColor(Color.BLACK);

XML

<AutoCompleteTextView
                android:id="@+id/autotext"
                android:layout_width="450dp"
                android:layout_height="50dp"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="40dp"
                android:background="@drawable/txt_field"
                android:dropDownHeight="300dp"
                android:hint="Enter Code"
                android:inputType="textCapCharacters"
                android:maxLength="5"
                android:paddingLeft="20dp"
                android:textColor="@android:color/primary_text_light"
                android:textSize="24sp" >
            </AutoCompleteTextView>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top