搜索ArrayList并将输出返回到另一个ArrayList

时间:2014-02-17 03:18:30

标签: android arraylist

我有一个“电影”对象的ArrayList 我想用“名字”搜索它,这是电影对象的一个​​属性

这是用于搜索的代码

     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总是等于零!

那是班级电影

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

}

该代码有什么问题?

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

2 个答案:

答案 0 :(得分:1)

尝试在电影名称的测试中使用equalsIgnoreCase。因为它是你必须有一个确切的案例匹配。

在将两个字符串转换为小写字母以进行包含搜索后,您可以考虑使用indexOf

答案 1 :(得分:0)

你确定“MovieReg_activity.movi​​es”不是空的吗?如果它为空,则匹配列表不会分配任何电影对象。这可能是你总是为matches.size()获得0的原因。检查并确保MovieReg_activity.movi​​es不为空。