AutoCompleteTextView建议重复

时间:2017-10-02 07:06:39

标签: android autocompletetextview

我的程序一直存在问题,当输入被输入autoCompleteTextView时,它会多次显示相同的建议。我真的可以用解决方案来防止这种情况发生。指向图像的链接是 如下:

enter image description here

这是一个赋值,其中存储在数据库中的值将用作autoCompleteTextView的建议。以下是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);

    }
}

以下是SQLiteOpenHelper类的代码:

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;
}

}

这是layout.xml文件(以防万一):

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

2 个答案:

答案 0 :(得分:3)

这是因为每次在创建Activity时插入数据都会发生这种情况。您应检查是否插入了数据,如果未插入数据,则需要将数据输入database。请尝试下面的代码

   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

答案 1 :(得分:0)

您应该database.insert_Data(companyNames[i]);中的MainActivityonCreate移至class myDatabase 例如:

@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]);

    }
}

您之前应该使用“Apple INK,...,Apple INK”清除(删除)带有大量记录的旧数据库。