How to implement a content provider with more than one table?

后端 未结 3 1947
傲寒
傲寒 2021-02-06 19:29

update: looking at \"vnd.android.cursor.dir/vnd.google.note\" and \"vnd.android.cursor.item/vnd.google.note\" it seemed to me as thoug

3条回答
  •  被撕碎了的回忆
    2021-02-06 20:07

    Here is my code for multiple table query in content provider with projectionMap

    //HashMap for Projection
     mGroupImageUri = new HashMap<>();
        mGroupImageUri.put(RosterConstants.JID,RosterProvider.TABLE_ROSTER+"."+RosterConstants.JID);
        mGroupImageUri.put(RosterConstants.USER_NAME,RosterProvider.TABLE_ROSTER+"."+RosterConstants.USER_NAME);
        mGroupImageUri.put(ChatConstants.MESSAGE,"c."+ChatConstants.MESSAGE+ " AS "+ ChatConstants.MESSAGE);
        mGroupImageUri.put(ChatConstants.SENDER,"c."+ChatConstants.SENDER+" AS "+ChatConstants.SENDER);
        mGroupImageUri.put(ChatConstants.URL_LOCAL,"c."+ChatConstants.URL_LOCAL+" AS "+ChatConstants.URL_LOCAL);
    
    //case for content type of uri
      case IMAGE_URI:
                qBuilder.setTables(RosterProvider.TABLE_ROSTER
                        + " LEFT OUTER JOIN "+ TABLE_NAME + " c"
                        + " ON c."+ ChatConstants.JID + "=" + RosterProvider.TABLE_ROSTER + "."+RosterConstants.JID);
                qBuilder.setProjectionMap(mGroupImageUri);
                break;
    
        //ContentResolver query for Projection form, selection and selection args
    String[] PROJECTION_FROM = new String[]{
                RosterConstants.JID,
                RosterConstants.USER_NAME,
                ChatConstants.MESSAGE,
                ChatConstants.SENDER,
                ChatConstants.URL_LOCAL
        };
    
        String selection = RosterProvider.TABLE_ROSTER +"."+RosterConstants.JID+ "='" + jid + "' AND " + "c."+ChatConstants.FILE_TYPE+"="+ChatConstants.IMAGE;
        String[] selectionArgu = null;
        String order = "c."+ChatConstants.MESSAGE+" ASC";
    
        Cursor cursor = mContentReolver.query(ChatProvider.CONTENT_URI_GROUP_IMAGE_URI,
                PROJECTION_FROM,selection, null,order);
    
        //@ChatProvider.CONTENT_URI_GROUP_IMAGE_URI = 'your content type uri'
        //@TABLE_NAME = 'table1'
        //@RosterProvider.TABLE_ROSTER ='table2'
    

提交回复
热议问题