如何改进自动完成文本小部件中的搜索

时间:2017-01-06 10:30:57

标签: android sqlite search autocompletetextview

我有两个使用sqlite数据库中的数据填充的AutoCompleteTextView。我从ERP服务器中提取信息并将其本地存储在sqlite数据库中,并填写textview,如下所示:

equipment = (AutoCompleteTextView) findViewById(R.id.equipmentAutoCompleteTextView);
    if(equipment.getText().toString().length() == 0){
        equipment.setError("Equipment is required");
    }
    FieldInstallationDB sqlitedb1 = new FieldInstallationDB(this);
    sqlitedb1.openForRead();

    String[] items = sqlitedb.getAllItemNames();

    for(int i = 0; i < items.length; i++)
    {
        Log.i(this.toString(), items[i]);
    }

    ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, items);
    equipment.setAdapter(adapter1);
    equipment.setThreshold(1);

在这里:

customerName = (AutoCompleteTextView) findViewById(R.id.customerNameAutoCompleteTextView);
    if(customerName.getText().toString().length() == 0){
        customerName.setError("Customer Name is required");
    }
    FieldInstallationDB sqlitedb = new FieldInstallationDB(this);
    sqlitedb.openForRead();

    String[] accounts = sqlitedb.getAllCustNames();

    for(int i = 0; i < accounts.length; i++)
    {
        Log.i(this.toString(), accounts[i]);
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, accounts);
    customerName.setAdapter(adapter);
    customerName.setThreshold(1);

假设我有这些:

  • 黑勺
  • Red Spoon
  • 一勺

我开始打字“spo”,我想看到列出的所有三个项目进行选择。相反,我只会出现“Spoonful”。如何让它识别并显示所有带有字母“spo”的实例(项目)?

更新

在对我的问题进行第一次评论后,我阅读了更多文章并更新了我的代码,如下所示:

equipment = (AutoCompleteTextView) findViewById(R.id.equipmentAutoCompleteTextView);
    if(equipment.getText().toString().length() == 0){
        equipment.setError("Equipment is required");
    }
    FieldInstallationDB sqlitedb1 = new FieldInstallationDB(this);
    sqlitedb1.openForRead();

    String[] items = sqlitedb1.getAllItemNames();

    for(int i = 0; i < items.length; i++)
    {
        Log.i(this.toString(), items[i]);
    }

    equipment.setThreshold(1);


    SimpleCursorAdapter itemNameAdapter = new SimpleCursorAdapter(
            this, android.R.layout.simple_dropdown_item_1line, null, items, toView, 0);



    itemNameAdapter.setFilterQueryProvider(new FilterQueryProvider() {

        public Cursor runQuery(CharSequence constraint) {
            return suggestItemCompletions(constraint);
        }
    });

itemNameAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
        public CharSequence convertToString(Cursor cur) {
            int index = cur.getColumnIndex(FieldInstallationDB.ITEM_NAME);
            return cur.getString(index);
        }});
    equipment.setAdapter(itemNameAdapter);

以下是在活动类中编写的suggestitemcompletions方法:

public Cursor suggestItemCompletions(CharSequence str) {

    return getContentResolver().query(null, new String[] {FieldInstallationDB.ITEM_NAME}, "(" + FieldInstallationDB.ITEM_NAME + " LIKE ? ", new String[] {"%" + str + "%"}, null);

}

这表明使用getContentResolver()在我的情况下没有意义,因为我正在访问我的应用程序的数据库而不是另一个应用程序。因此,它不起作用,因为我将URI参数设置为null,因为我的数据库表没有URI。但是,如果我决定不使用getContentResolver()并直接查询FieldInstallationDB,我会收到.query()的错误,说无法解析方法。

更新II:

我按照建议使用了rawQuery(),它可以工作:

public Cursor suggestItemCompletions(CharSequence str) {
    fieldInstDatabase = openForRead();
    String sql = "Select "+ ITEM_NAME+ " FROM "+ TABLE_EQUIPMENT+ " WHERE "+ ITEM_NAME + " LIKE ?";
    String[]  selectArgs = new String[]{ "%" + str + "%"};

    return fieldInstDatabase.rawQuery(sql,selectArgs);

}

这个方法在我的db类中,我在这里的主要活动中调用它:

itemNameAdapter.setFilterQueryProvider(new FilterQueryProvider() {

        public Cursor runQuery(CharSequence constraint) {
            return sqlitedb1.suggestItemCompletions(constraint);
        }
    });

这是有效的,除了我在这里得到一个IllegalArgumentException:

  

01-10 13:07:08.792 5361-5361 / com.example.sweetiean.stlfieldinstallation1 E / AndroidRuntime:FATAL EXCEPTION:main                                                                                              进程:com.example.sweetiean.stlfieldinstallation1,PID:5361                                                                                              java.lang.IllegalArgumentException:列'Bag(TypeC):                                                                                              5900016009'不存在

这是有道理的,因为“Bag(TypeC):                                                                                      5900016009“不是列,它是数据库中ITEM NAME列中的第一项。

我不知道为什么它会读取第一项作为列名。我试过调试,但我不能介入

public Cursor runQuery(CharSequence constraint) {
        return sqlitedb1.suggestItemCompletions(constraint);
    }

1 个答案:

答案 0 :(得分:0)

正如评论中所建议的那样,由于SimpleCursorAdapter中的from参数,因此发生了IllegalArgumentException。 SimpleCursorAdapter的fromto参数需要分别填充表的列名和textview。

在我的情况下,我使用从数据库中获取的项目的字符串数组填充from参数:

String[] items = sqlitedb1.getAllItemNames();

for(int i = 0; i < items.length; i++)
{
    Log.i(this.toString(), items[i]);
}

equipment.setThreshold(1);


SimpleCursorAdapter itemNameAdapter = new SimpleCursorAdapter(
        this, android.R.layout.simple_dropdown_item_1line, null, items, toView, 0);

将其更正为:

SimpleCursorAdapter itemNameAdapter = new SimpleCursorAdapter(
        this, android.R.layout.simple_dropdown_item_1line, null, fromCol, toView, 0);

fromCol定义为final static String[] fromCol = new String[] { FieldInstallationDB.ITEM_NAME };修复它。