Reputation: 41
I keep having an issue with my program where when the input is given into an autoCompleteTextView, it shows the same suggestions multiple times. I really could do with solutions to prevent this from happening. A link to an image is given below:
This is for an assignment where values stored in a database are to be used as suggestions for the autoCompleteTextView. Here is the code for the MainActivity:
package com.example.ayush.sqliteautocompleteapplication;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String[] companyNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDatabase database = new myDatabase(this);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
companyNames = new String[]{"Apple INC", "Samsung Electronics", "Amazon.com", "Microsoft", "Hitachi", "IBM", "Sony", "Huawei", "Panasonic", "Hewlett Packard Enterprises"};
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < companyNames.length; i++) {
database.insert_Data(companyNames[i]);
}
Cursor cursor = database.showProduct();
while (cursor.moveToNext()) {
list.add(cursor.getString(cursor.getColumnIndex("name")));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list);
textView.setAdapter(adapter);
}
}
Following is the code for the SQLiteOpenHelper class:
package com.example.ayush.sqliteautocompleteapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by ayush on 10/2/2017.
*/
public class myDatabase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contactsManager";
private static final String TABLE_COMPANIES = "companies";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public myDatabase(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_COMPANIES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" +
")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public boolean insert_Data(String name)
{
SQLiteDatabase db=getWritableDatabase(); //to write data into the table
ContentValues values=new ContentValues();
values.put(KEY_NAME,name); //putting the values into ContentValue
long res=db.insert(TABLE_COMPANIES,null,values); //Create a temp variable
if(res==-1)
{
return false; //if data is not inserted return false
}
else
{
return true; //else return true
}
}
public Cursor showProduct() {
//fetching data
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from companies", null);
if (cursor != null)
return cursor;
else
return null;
}
}
And here is the layout.xml file(Just in case):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal"
tools:context="com.example.ayush.sqliteautocompleteapplication.MainActivity">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/autoComplete"
android:completionThreshold="1"
/>
</LinearLayout>
Upvotes: 3
Views: 566
Reputation: 2067
This happens because everytime you insert the data when Activity
is created. You should check whether data is inserted or not and if data is not inserted then you need to enter data into database
. Try below code
Cursor cursor = database.showProduct();
if(cursor.getCount() == 0){
for (int i = 0; i < companyNames.length; i++) {
database.insert_Data(companyNames[i]);
}
cursor = database.showProduct();
}
//Continue with your code
Upvotes: 3
Reputation: 3732
You should move database.insert_Data(companyNames[i]);
from MainActivity
to onCreate
in class myDatabase
For example:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_COMPANIES + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" +
")";
db.execSQL(CREATE_CONTACTS_TABLE);
companyNames = new String[]{"Apple INC", "Samsung Electronics", "Amazon.com", "Microsoft", "Hitachi", "IBM", "Sony", "Huawei", "Panasonic", "Hewlett Packard Enterprises"};
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < companyNames.length; i++) {
database.insert_Data(companyNames[i]);
}
}
You should clear(delete) old database with a lot of record with "Apple INK, ..., Apple INK" before.
Upvotes: 0