机器人忽略的搜索功能不存在的关键字 [英] android ignore non existing keywords in search function

查看:169
本文介绍了机器人忽略的搜索功能不存在的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hye..I设法创造搜索功能使用多个关键字进行搜索。问题是必须的关键字,我已经把我的数据库列里面存在。如果我用搜索关键字3这是2个关键字匹配的内部数据库中,但1个关键字是不存在的。结果是没有任何显示。

我怎么能忽略不存在的关键字?只是把那只是我的数据库中存在的关键字。意味着,如果用户输入的关键字,系统只会读取仅匹配我的数据库中的关键字,它会忽略不匹配的关键字。下面是我的code。

 一个String = txtLelaki.getText()的toString()。
                字符串s = txtLelaki.getText()的toString()。
                的String []字= s.split();
                SQL字符串= NULL;                字符串,其中= helper.KEY_LELAKI +=;
                串关系=或;
                字符串wildcard1 =%;
                字符串wildcard2 =%';                StringBuilder的构建= NULL;
                的for(int i = 0; I< words.length;我++)
                {
                    字由[i] =新的StringBuilder(wildcard1).append(字由[i])追加(wildcard2)的ToString();
                    如果(我== 0)
                        打造=新的StringBuilder(其中);
                    其他
                        build.append(关系).append(其中);
                }                光标C = database.query(helper.DB_TABLE_LELAKI,空,build.toString(),也就是说,NULL,NULL,NULL);
                如果(C!= NULL)
                {
                    c.moveToFirst();
                    INT soalanColumn = c.getColumnIndex(helper.MAS​​ALAH_LELAKI);
                    INT的getId = c.getColumnIndex(helper.ID_LELAKI);
                    如果(c.isFirst())
                    {
                        做
                        {
                            idList.add(c.getLong(的getId));
                            results.add(c.getString(soalanColumn));
                        }而(c.moveToNext());
                    }                }                adapter.updateList(结果);
                listView.setTextFilterEnabled(真);            } //结束试                赶上(的SQLException E)
                {
                    Log.v(CAER,e.toString());
                }


解决方案

重写结果
我还是不明白,正是你想做的事,但看着你的previous问题:包含多个关键字<机器人搜索/一>,我假设你想这样的查询: SELECT * FROM表其中foo是怎样的?或富喜欢? ... 为每个可能的关键字。你需要建立这种 WHERE 子句自己:

 字符串,其中= helper.KEY_LELAKI +=;
字符串关系=或;
字符串通配符=%;StringBuilder的建设者;
的for(int i = 0; I&LT; words.length;我++){
    //环绕关键字用%,即猫%%
    字由[i] =新的StringBuilder(通配符).append(字由[i])追加(通配符)的ToString();    //构建WHERE子句
    如果(我== 0)
        建设者=新的StringBuilder(其中);
    其他
        builder.append(关系).append(其中);
}光标C = myDataBase.query(helper.DB_TABLE_LELAKI,空,
        builder.toString(),也就是说,NULL,NULL,NULL);

如果取值在你的例子是猫胡子拉您的查询将有一个包含关键字的每一行短语猫,胡子,或拉。

Hye..I managed to create search function to search using multiple keywords. The problem is the keywords must be exist inside my database column that i had set. If i search using 3 keywords which is 2 keywords match inside database but 1 more keyword is not exist. The result was displayed nothing.

How could I ignore the non existing keywords?? just take the keywords that exists inside my database only. Means, if the user enter many keywords, system will read only keywords that match inside my database only and it will ignore the keywords that doesn't match. Below is my code.

String s = txtLelaki.getText().toString();
                String s = txtLelaki.getText().toString();
                String[] words = s.split(" ");
                String sql = null;

                String where = helper.KEY_LELAKI + " = ?";
                String relation = " or ";
                String wildcard1 = "'%";
                String wildcard2 = "%'";

                StringBuilder build = null;
                for(int i=0;i<words.length; i++)
                {
                    words[i] = new StringBuilder(wildcard1).append(words[i]).append(wildcard2).toString();
                    if(i==0)
                        build = new StringBuilder(where);
                    else
                        build.append(relation).append(where);                       
                }

                Cursor c = database.query(helper.DB_TABLE_LELAKI, null, build.toString(), words, null, null, null);


                if(c!= null)
                {
                    c.moveToFirst();
                    int soalanColumn = c.getColumnIndex(helper.MASALAH_LELAKI);
                    int getId = c.getColumnIndex(helper.ID_LELAKI);
                    if(c.isFirst())
                    {
                        do
                        {
                            idList.add(c.getLong(getId));
                            results.add(c.getString(soalanColumn));


                        }while(c.moveToNext());
                    }

                }

                adapter.updateList(results);
                listView.setTextFilterEnabled(true);

            }//end try

                catch(SQLException e)
                {
                    Log.v("caer", e.toString());    
                }

解决方案

Rewrite
I still don't understand exactly what you want to do, but looking at your previous question:
android searching with multiple keywords, I assume that you want a query like this: SELECT * FROM Table WHERE foo like ? OR foo like ? ... for each of your possible keywords. You need to build this WHERE clause yourself:

String where = helper.KEY_LELAKI + " = ?";
String relation = " OR ";
String wildcard = "%";

StringBuilder builder;
for(int i = 0; i < words.length; i++) {
    // Surround the keyword with "%", i.e. "%cat%"
    words[i] = new StringBuilder(wildcard).append(words[i]).append(wildcard).toString();

    // Build the WHERE clause
    if(i == 0)
        builder = new StringBuilder(where);
    else
        builder.append(relation).append(where);
}

Cursor c = myDataBase.query(helper.DB_TABLE_LELAKI, null, 
        builder.toString(), words, null, null, null);

If s in your example is "cat mustache pull" your query will have every row that contains the keyword phrases "cat", "mustache", or "pull".

这篇关于机器人忽略的搜索功能不存在的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆