0

I am working on a android application with sqlite database that store lots of standard english texts that have uppercase and lowercase words and it shows on the app, we have a search that right now is case sensitive and scroll between them with cursor

How I can return search string case insensitive from sqlite

this is my code on database

public List<String> getSearchedParagraph(String p) {
    String sectionId = "";
    List<String> result = new ArrayList<String>();
    String query = "SELECT Distinct(sectionId)  FROM paragraph where txt Like '%"
            +p+ "%'";
    Cursor cursor = db.rawQuery(query, null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            result.add(cursor.getString(0));
            cursor.moveToNext();

        }
    }
    return result;
}

i tested String query = "SELECT Distinct(sectionId) FROM paragraph where txt Like '%" +p+ "%' COLLATE NOCASE"; but still no change

UPDATE1 this is setWordSearch function:

    public void setWordSearch(String str) {
    wordSearch = str;
    for (FragmentPage page : fragments) {
        page.wordSearch = str;
    }
    notifyDataSetChanged();
}

UPDATE2 This is the FragmentPage:

public class FragmentPage extends Fragment {
VasiatClass v;
public String SectionId;
public List<VasiatClass> paragraph;
public String wordSearch;
private ScrollView sv;
static MediaPlayer player = new MediaPlayer();
View view;
private List<RelativeLayout> jtxtvwList = new ArrayList<RelativeLayout>();
RelativeLayout rl = null;
float position_x = 0, position_y = 0;
private int playingParagraphPosition;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.SectionId = String.valueOf(getArguments().getInt("sectionId"));
    this.wordSearch = getArguments().getString("wordSearch");
    Log.i("FRAGMENTPAGE",
            "sectionId is :"
                    + String.valueOf(getArguments().getInt("sectionId")));
    DataBaseHelper myDbHelper = new DataBaseHelper(this.getActivity());
    myDbHelper = new DataBaseHelper(this.getActivity());
    wordSearch = getArguments().getString("wordSearch");
    try {

        myDbHelper.createDataBase();
        myDbHelper.openDataBase();
        this.paragraph = myDbHelper.getParagraph(SectionId);
        myDbHelper.close();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");
    }

};

public String getSection() {
    return SectionId;
}

public FragmentPage() {
    // TODO Auto-generated constructor stub
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_vasiatpage, container,
            false);
    this.view = view;
    LinearLayout ll = (LinearLayout) view.findViewById(R.id.llId);
    ll.setPadding(20, 20, 20, 20);
    LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    linLayoutParam.setMargins(20, 20, 20, 20);
    sv = (ScrollView) view.findViewById(R.id.scrollView1);
    for (int i = 0; i <= paragraph.size() - 1; i++) {
        final int j = i;
        final RelativeLayout rel = new RelativeLayout(getActivity());
        // Button b = new Button(getActivity());
        // b.setBackgroundColor(Color.TRANSPARENT);
        // b.setTextColor(Color.TRANSPARENT);
        JustifiedTextView textView = new JustifiedTextView(getActivity());

        /************************************/
        textView.setOnTouchListener(new OnTouchListener() {
            public final static int FINGER_RELEASED = 0;
            public final static int FINGER_TOUCHED = 1;
            public final static int FINGER_DRAGGING = 2;
            public final static int FINGER_UNDEFINED = 3;
            private int fingerState = FINGER_RELEASED;

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                int action = arg1.getAction();

                boolean check = true;
                switch (action) {
                case MotionEvent.ACTION_DOWN:

                    position_x = arg1.getX();
                    position_y = arg1.getY();
                    Log.i("ACTIONS", "Ation down -- position_x:"
                            + position_x + " ,position_Y" + position_y);

                    check = true;
                    if (fingerState == FINGER_RELEASED)
                        fingerState = FINGER_TOUCHED;
                    else
                        fingerState = FINGER_UNDEFINED;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (fingerState == FINGER_TOUCHED
                            || fingerState == FINGER_DRAGGING)
                        fingerState = FINGER_DRAGGING;
                    else
                        fingerState = FINGER_UNDEFINED;

                    check = false;

                    break;
                case MotionEvent.ACTION_UP:
                    Log.i("ACTIONS",
                            "Ation up -- position_x:" + arg1.getX()
                                    + " ,position_Y" + arg1.getY());

                    if (Math.abs((arg1.getX() - position_x)) < 10
                            && Math.abs((arg1.getY() - position_y)) < 10) {
                        List<String> audioList = new ArrayList<String>();
                        playingParagraphPosition = j;
                        ((VasiatText) getActivity())
                                .playParagraph(paragraph.get(j)
                                        .getFilename() + "_MP3.mp3", j,
                                        FragmentPage.this);

                        if (rl != null) {
                            rl.setBackgroundColor(Color.TRANSPARENT);
                        }

                        rel.setBackgroundColor(Color.LTGRAY);
                        rl = rel;
                    }

                    break;

                default:
                    fingerState = FINGER_UNDEFINED;
                    break;
                }
                return false;
            }
        });
        /********************************************/

        // textView.getSettings().setFixedFontFamily("Swissra Light.otf");

        String str = paragraph.get(i).getTxt().toString();
        int subStringStart = 0;
        int startIndex = 0;
        StringBuilder sb = new StringBuilder();

        if (wordSearch != null && !wordSearch.equals("")) {
            startIndex = str.indexOf(wordSearch);
            while (startIndex != -1) {

                sb.append(str.substring(subStringStart, startIndex)
                        + "<span style=\"background-color:#ffff00\">"
                        + str.substring(startIndex,
                                startIndex + wordSearch.length())
                        + "</span>");
                subStringStart = startIndex + wordSearch.length();

                startIndex = str.indexOf(wordSearch, subStringStart);
            }
            sb.append(str.substring(subStringStart));
        }
        if (!sb.toString().equals("")) {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                textView.setText(sb.toString());
            } else
                textView.setTextNoJustify(sb.toString());
            // b.setText(sb.toString());
        } else {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                textView.setText(str);
            } else
                textView.setTextNoJustify(str);
            // b.setText(str);
        }
        textView.setId(i);
        textView.setTag(Integer.valueOf(paragraph.get(i).getId()));
        rel.addView(textView);



        jtxtvwList.add(rel);
        ll.addView(rel);
    }

    return view;
}

thanks

n3tg33k
  • 75
  • 1
  • 10
  • LIKE is case insensitive (for ASCII data). What exactly is the problem? – CL. Mar 01 '16 at 13:09
  • @CL. yeah i am using LIKE but it seems LIKE is case sensitive on android!!! – n3tg33k Mar 01 '16 at 15:56
  • It's not. Show an example. – CL. Mar 01 '16 at 16:11
  • @CL. it doesn't work on my case , I need to highlight "about" and "About" in my text when i search "about"... here is the java part >> `mPagerAdapter.setWordSearch(edt.getText().toString());` – n3tg33k Mar 01 '16 at 16:28
  • So you're actually asking about a different part of your code, which has nothing to do with LIKE, and which you haven't shown in the question? – CL. Mar 01 '16 at 17:59
  • @CL. oh right now i feel like i don't know the right question!!! the thing is i want to search in my database case insensitive and i cant do it and every solution i find won't work! actually i found a workaround but its not a solution : "to lowercase the entire database and do mPagerAdapter.setWordSearch(edt.getText().toString().toLowercase()); " on the input part but then the text is all lowercase and its not acceptable – n3tg33k Mar 01 '16 at 18:35
  • What is that `setWordSearch` you keep talking about? – CL. Mar 01 '16 at 18:39
  • @CL. `setWordSearch` is the variable that get the searched string from client., but i executed the query directly to sqlite database it show me results case-insensitive, I guess the problem is with Cursor it highlight case-sensitive!! – n3tg33k Mar 02 '16 at 07:06
  • No, a function is not a variable. And if you insist of keeping it secret, nobody can help you. – CL. Mar 02 '16 at 08:31
  • @CL. yes sorry its a function here is the function: ` public void setWordSearch(String str) { wordSearch = str; for (FragmentPage page : fragments) { page.wordSearch = str; } notifyDataSetChanged(); }` – n3tg33k Mar 02 '16 at 12:05
  • @CL. I updated my question thanks – n3tg33k Mar 02 '16 at 12:16

3 Answers3

1

You are using String.indexOf for searching. This function is case sensitive.

You need to search for the substring while ignoring case.

Community
  • 1
  • 1
CL.
  • 173,858
  • 17
  • 217
  • 259
1

as @CL mentioned, you can use Patterns,

just replace this code:

    if (wordSearch != null && !wordSearch.equals("")) {
        startIndex = str.indexOf(wordSearch);
        while (startIndex != -1) {

            sb.append(str.substring(subStringStart, startIndex)
                    + "<span style=\"background-color:#ffff00\">"
                    + str.substring(startIndex,
                            startIndex + wordSearch.length())
                    + "</span>");
            subStringStart = startIndex + wordSearch.length();

            startIndex = str.indexOf(wordSearch, subStringStart);
        }
        sb.append(str.substring(subStringStart));
    }

whit this:

Boolean wasFound;

if (wordSearch != null && !wordSearch.equals("")) {
        Matcher subMatcher = Pattern.compile(Pattern.quote(wordSearch), Pattern.CASE_INSENSITIVE).matcher(str);

        wasFound = subMatcher.find();
    while (wasFound) {

        startIndex = subMatcher.start();
        sb.append(str.substring(subStringStart, startIndex)
            + "<span style=\"background-color:#ffff00\">"
            + str.substring(startIndex,
            startIndex + wordSearch.length())
            + "</span>");

        subStringStart = startIndex + wordSearch.length();
        wasFound = subMatcher.find();
    }

    sb.append(str.substring(subStringStart));
}

I hope that this help you @n3tg33k ;)

Community
  • 1
  • 1
-1

you can achieve it in this way....

public List<String> getSearchedParagraph(String p) {
    p=p.toLowerCase();
    String sectionId = "";
    List<String> result = new ArrayList<String>();
    String query = "SELECT Distinct(sectionId)  FROM paragraph where LOWER(txt) Like '%"
            +p+ "%'";
    Cursor cursor = db.rawQuery(query, null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            result.add(cursor.getString(0));
            cursor.moveToNext();

        }
    }
    return result;
}
H Raval
  • 1,903
  • 17
  • 39