0

i have an ArrayList of "movie" object i wanted to search in it by "name" which is one of the attributes of the movie object

thats the code used for searching

     ArrayList<movie> matches = new ArrayList<movie>();
                // go through list of members and compare name with given name
                for(movie movie : MovieReg_activity.movies) {
                Log.d("searchBar", searchbar); 
                Log.d("Movie Name", movie.getName());
                    if (movie.getName().equalsIgnoreCase(searchbar)) {
                        matches.add(movie); // adds matching member to the return list
                    }
                }
                int match_size=matches.size();
                tv.setText("no of matches=" +match_size);

match_size always equals to zero !

thats the class movie

public class movie {

    String name;
    int dvd_no ;

    public movie( String name1 , int dvd_no1) 
    {
                this.name = name1 ;
            this.dvd_no = dvd_no1 ; 

    }

    String getName()
    {
        return this.name ;
    }

}

what is the problem with that code?

logcat

02-17 04:28:38.500: I/Process(638): Sending signal. PID: 638 SIG: 9
02-17 04:30:27.020: W/KeyCharacterMap(720): No keyboard for id 0
02-17 04:30:27.020: W/KeyCharacterMap(720): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 04:43:35.531: W/KeyCharacterMap(747): No keyboard for id 0
02-17 04:43:35.531: W/KeyCharacterMap(747): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 04:55:23.350: W/KeyCharacterMap(774): No keyboard for id 0
02-17 04:55:23.360: W/KeyCharacterMap(774): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 04:58:04.050: W/KeyCharacterMap(801): No keyboard for id 0
02-17 04:58:04.050: W/KeyCharacterMap(801): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 04:59:40.970: W/KeyCharacterMap(828): No keyboard for id 0
02-17 04:59:40.970: W/KeyCharacterMap(828): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 05:01:25.971: W/KeyCharacterMap(881): No keyboard for id 0
02-17 05:01:25.971: W/KeyCharacterMap(881): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 05:01:49.500: W/IInputConnectionWrapper(881): showStatusIcon on inactive InputConnection
02-17 05:02:54.722: W/KeyCharacterMap(908): No keyboard for id 0
02-17 05:02:54.722: W/KeyCharacterMap(908): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 05:24:44.560: W/KeyCharacterMap(935): No keyboard for id 0
02-17 05:24:44.560: W/KeyCharacterMap(935): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 05:27:29.980: W/KeyCharacterMap(962): No keyboard for id 0
02-17 05:27:29.980: W/KeyCharacterMap(962): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-17 05:27:37.289: D/searchBar(962): fawzy
02-17 05:29:33.540: D/searchBar(962): fawzy
17
  • is case sensitivity important? if not, then change your if statment to if (movie.getName().equalsIgnoreCase(searchbar)){... Commented Feb 17, 2014 at 3:21
  • Are you sure searchbar is what you want to search for? Is its type a String? Or did you want to do searchbar.getText() or the like? Commented Feb 17, 2014 at 3:21
  • or even searchbar.getText().trim(). Did you try debugging through your code? Commented Feb 17, 2014 at 3:23
  • @panini even it's the same word exactly always equals zero Commented Feb 17, 2014 at 3:23
  • 1
    Before your If Statement, Log.d("searchBar", searchBar); Log.d("Movie Name", movie.getName()"); Will show you what it is comparing. Commented Feb 17, 2014 at 3:24

2 Answers 2

1

Try using equalsIgnoreCase in the test for the movie name. As it is you must have an exact case match.

And you might consider using indexOf after converting both strings to lower case to do a contains search.

Sign up to request clarification or add additional context in comments.

2 Comments

Then add a debug print of the two values in the loop and ensure they are what you expect.
Movie.getName doesn't return !
0

Are you sure that "MovieReg_activity.movies" is not empty? If it is empty your matches list does not assign any movie objects. That might be the reason always you get 0 for matches.size(). Check and make sure MovieReg_activity.movies is not empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.