HotMovie

2016.10.21:

之前的更新都没有贴出来。接下来对应用还会有一些重构,每隔一阶段将更新工作总结一下贴上来,顺路学习下git的命令行操作。

Version:2.2

地址:https://github.com/yusband/HotMovie/tree/HotMovie2.2

New Features:

  • 新增了收藏功能,可以在详情页点击收藏,将电影海报缓存至手机存储卡,在主页面的显示模式中点击“我的收藏”即可获得收藏过电影的列表

遇到的坑:

  • 如何看到sqliteDatabase中存储的内容
  1. 使用adb shell工具,通过命令行进入 data/data/包名/databases 之后,出现opendir failed, Permission denied的错误提示
  2. 根据http://stackoverflow.com/questions/19194576/how-do-i-view-the-sqlite-database-on-an-android-device该链接下列出的方法并没有解决问题;采用真机测试(小米5s,api 23)
  3. 最终的解决办法是没有尝试root手机(小米root需要申请...),将程序在avm上运行,直接在DDMS下找到.db文件,在火狐浏览器的SQLite Manager中查看
  • UriMatcher无法识别Uri的bug
  1. private static final int MOVIE =100 ;
        private static final int MOVIE_ALL =200 ;
        private static final int MOVIE_CERTAIN =300 ;
        private OpenHelper mOpenHelper;
        static UriMatcher buildUriMatcher() {
    
            UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
    
            final String authority=MovieContact.CONTENT_AUTHORITY;
            matcher.addURI(authority,MovieContact.PATH_MOVIE, MOVIE);
            matcher.addURI(authority,MovieContact.PATH_MOVIE+"/*",MOVIE_ALL);
            matcher.addURI(authority,"movie/#",MOVIE_CERTAIN);
    public int delete(Uri uri, String s, String[] strings) {
            final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
            final int match = sUriMatcher.match(uri);
            int deleteRows=0;
    
            switch (match) {
                case MOVIE_CERTAIN: {
    
                   deleteRows=db.delete(MovieContact.MovieEntry.TABLE_NAME,s,strings);
                    if ( deleteRows<= 0 )
                        throw new android.database.SQLException("Failed to insert row into " + uri);
                    break;
                }
                case MOVIE_ALL: {
    
                    Log.i("movie_all","");
                }
                case  MOVIE:{
                    Log.i("movie","");
                }
                default:
                    throw new UnsupportedOperationException("Unknown uri: " + uri);
            }
            getContext().getContentResolver().notifyChange(uri, null);
            return deleteRows;
    }

    以上是继承自ContentProvider类中的UriMatcher和delete方法,运行会抛出 预定义的UnsupportedOperationException异常

  2. 参考http://stackoverflow.com/questions/5030094/problems-with-androids-urimatcher/15015687#15015687,uriMatcher的识别同定义时addUri的顺序有关:

          应当避免在addUri时将*/# 与 */*放在相邻的两行,这里尽管在addUri中定义了如图所示的uri,当match时会因为CONTENT_URI/*在前,而且segment形式一致,会直接返回match CONTENT_URI/*的结果,会抛出异常。

          当在初始化uriMatcher()的过程中,将CONTENT_URI/*和CONTENT_URI/#的顺序交换,便不会抛出异常;这也提醒在定义UriMatcher的过程中,需要注意顺序,格式可以参考官方文档:

sURIMatcher.addURI("contacts", "people", PEOPLE);
        sURIMatcher.addURI("contacts", "people/#", PEOPLE_ID);
        sURIMatcher.addURI("contacts", "people/#/phones", PEOPLE_PHONES);
        sURIMatcher.addURI("contacts", "people/#/phones/#", PEOPLE_PHONES_ID);
        sURIMatcher.addURI("contacts", "people/#/contact_methods", PEOPLE_CONTACTMETHODS);
        sURIMatcher.addURI("contacts", "people/#/contact_methods/#", PEOPLE_CONTACTMETHODS_ID);
        sURIMatcher.addURI("contacts", "deleted_people", DELETED_PEOPLE);
        sURIMatcher.addURI("contacts", "phones", PHONES);
        sURIMatcher.addURI("contacts", "phones/filter/*", PHONES_FILTER);
        sURIMatcher.addURI("contacts", "phones/#", PHONES_ID);
        sURIMatcher.addURI("contacts", "contact_methods", CONTACTMETHODS);
        sURIMatcher.addURI("contacts", "contact_methods/#", CONTACTMETHODS_ID);
        sURIMatcher.addURI("call_log", "calls", CALLS);
        sURIMatcher.addURI("call_log", "calls/filter/*", CALLS_FILTER);
        sURIMatcher.addURI("call_log", "calls/#", CALLS_ID);

 

 

-----------------------------------------------------------------------------------分割线-----------------------------------------------------------------------------------

增加了一下几个功能:

1、加载离线评论(使用了sqlite间接存储了一整个list<String>里面的数据)

2、优化了横版布局,在横屏后使用两个fragment同屏(复习了一下含有fragment的布局以及fragment通过activity的交互)

 

detail:

1:

如图,评论列表对应着一个获取到的List<String>;而要实现在没有网络的情况下对评论的加载,就需要将每部电影对应的评论列表保存到sqlite中。

sqlite不支持List以及数组的直接存储,但是在stackoverflow上发现一种处理方式可以绕过这个限制。

http://stackoverflow.com/questions/9053685/android-sqlite-saving-string-array

在类中定义两个静态方法,完成String数组和String的转换。(List<String>和String数组的转换比较简单,略过)

public static String strSeparator = "__,__";
public static String convertArrayToString(String[] array){
    String str = "";
    for (int i = 0;i<array.length; i++) {
        str = str+array[i];
        // Do not append comma at the end of last element
        if(i<array.length-1){
            str = str+strSeparator;
        }
    }
    return str;
}
public static String[] convertStringToArray(String str){
    String[] arr = str.split(strSeparator);
    return arr;
}

2:横版布局的优化中复习了有关fragment的若干问题

  • fragment的静态/动态加载
  • fragment之间的通信    
posted @ 2016-10-21 15:26  派大东  阅读(897)  评论(0编辑  收藏  举报