android edittext autocomplete from sqlite database
In Android, AutoCompleteTextView is a view i.e similar to EditText, except that it displays a list of completion suggestions automatically while the user is typing. A list of suggestions is displayed in drop down menu from which user can choose an item which actually replace the content of Editbox with that.
AutoCompleteTextView in Android development is an editable text field that once a certain number of characters are entered, displays a drop down list showing auto-completion results that the user can select from to save time typing.
MainActivity.java:
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.akash.dairyapp.animal.Animal_Add;
import com.akash.dairyapp.databasea.Controllerdb;
import com.akash.dairyapp.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView edcustfat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edcustfat=(AutoCompleteTextView)findViewById(R.id.edcustfat);
fatauto();
}
public void fatauto() {
try {
final String[] mydata;
ArrayList<String> array = new ArrayList<>();
//Inside the method you've read the cursor, loop through it and add those item to array
String sql = "SELECT Fat_id,Fat,Rate FROM Fat_Table";
//execute SQL
Cursor cr = db.rawQuery(sql, null);
cr.moveToFirst();//cursor pointing to first row
mydata = new String[cr.getCount()];//create array string based on numbers of row
int i = 0;
do {
mydata[i] = cr.getString(1);//insert new stations to the array list
//Log.i("ArrayList",mydata[i]);
i++;
} while (cr.moveToNext());
//Finally Set the adapter to AutoCompleteTextView like this,
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, mydata);
//populate the list to the AutoCompleteTextView controls
edcustfat.setAdapter(adapter);
} catch (Exception e) {
Log.e(""," ");
}
}
}
actvity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/txttvbill"
android:layout_width="107dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="18sp"
android:text="Fat" />
<AutoCompleteTextView
android:id="@+id/edcustfat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal|numberSigned"/>
</LinearLayout>
</LinearLayout>
No comments:
Post a Comment