android ignore non existing keywords in search function

≡放荡痞女 提交于 2020-02-06 08:49:33

问题


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

回答1:


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".



来源:https://stackoverflow.com/questions/15162564/android-ignore-non-existing-keywords-in-search-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!