Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从数据库获取完成建议_Android_Database_Autocomplete - Fatal编程技术网

Android 从数据库获取完成建议

Android 从数据库获取完成建议,android,database,autocomplete,Android,Database,Autocomplete,我试图从数据库中检索前5个单词,并将它们设置为按钮的文本,这是我的代码 String [] deal; String text,s1,s2,s3,s4,s5; text=edt.getText().toString(); deal=db.getAllItemFilter(text); s1=deal[0]; s2=deal[0]; s3=deal[0];

我试图从数据库中检索前5个单词,并将它们设置为按钮的文本,这是我的代码

String [] deal;
 String text,s1,s2,s3,s4,s5;
 text=edt.getText().toString();
         deal=db.getAllItemFilter(text);
                 s1=deal[0];
                 s2=deal[0];
                 s3=deal[0];
                 s4=deal[0];
                 s5=deal[0];

                 sug1.setText(s1);
                 sug2.setText(s2);
                 sug3.setText(s3);
                 sug4.setText(s4);
                 sug5.setText(s5);
对于数据库查询,我正在使用此代码,但它不起作用

public String[] getAllItemFilter(String text)
    {
        String [] columns=  new String[]{word};

        Cursor cursor = this.ourdatabase.query(database_table, columns, " word like 'text%' ", null, null, null, null);

        String [] deal = new String[cursor.getCount()];

            int iword = cursor.getColumnIndex(word);
            int i=0;
            for (cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
            {
                 deal[i]=cursor.getString(iword);
                 i++;
                 if(i==5)
                     break;
             }

            return deal;

    }

有谁能帮我从数据库中获得前5条建议吗?提前谢谢你,你应该先在SQL查询端设置一个“LIMIT=5”,而不是循环限制,这样可以节省资源。然后您可以将单词%替换为%word%,以匹配单词开头。

您的查询不正确。设置好后,它每次都在查找“text*”(您忘记了设置查询格式,因此该变量被用作变量)

如果希望单词以所提供的文本开头,请将光标更改为我在下面显示的内容:

Cursor cursor = this.ourdatabase.query(database_table, columns, " word like '" + text + "%' ", null, null, null, null); 
如果只需要包含所提供文本的单词,请将其更改为:

Cursor cursor = this.ourdatabase.query(database_table, columns, " word like '%" + text + "%' ", null, null, null, null); 

为什么叫这个名字?