IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

ContentProvider avec plusieurs tables


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Inscrit en
    Septembre 2010
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Septembre 2010
    Messages : 3
    Par défaut ContentProvider avec plusieurs tables
    Bonjour,

    Je suis à la recherche d'un exemple démontrant l'implémentation d'un ContentProvider contenant différentes tables.

    La documentation officielle annonce que c'est possible mais n'illustre pas vriament son implémentation.

    J'imagine qu'il faudrait utiliser un Uri par table et faire un switch sur l' Uri passé en paramètre aux methodes: insert, delete, update...

    Pourriez-vous m'éclairer ?

    Merci

  2. #2
    Membre confirmé
    Inscrit en
    Février 2005
    Messages
    127
    Détails du profil
    Informations forums :
    Inscription : Février 2005
    Messages : 127
    Par défaut
    Bonjour,

    voila un exemple de code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    public class Provider extends ContentProvider {
        private static final String DATABASE_NAME = "basedonnee.db";
        private static String DATABASE_PATH = "/data/data/(package)/databases/";
        private static String DATABASE_TABLE_TABLE1 = "table1";
        private static String DATABASE_TABLE_TABLE2 = "table2";
        private static int DATABASE_VERSION = 1;
     
    	private static final int CONSTANTS=1;
    	private static final int CONSTANT_ID=2;
    	private static final UriMatcher MATCHERtable1;
    	private static final UriMatcher MATCHERtable2;
    	private static HashMap<String, String> TABLE1_LIST_PROJECTION;
    	private static HashMap<String, String> TABLE2_LIST_PROJECTION;
     
    	public static final class table1 implements BaseColumns {
    		public static final Uri CONTENT_URI
    				 =Uri.parse("content://(package)/table1");
     
    		public static final String DEFAULT_SORT_ORDER_NOM = "Nom";
    		public static final String DEFAULT_SORT_ORDER_MATRICULE = "Matricule";
    	    	public static final String _ID = "_id";		
    		public static final String MATRICULE = "Matricule";
    		public static final String NOM = "Nom";
    		public static final String PRENOM = "Prenom";
    	}
     
    	static {
    		MATCHERtable1=new UriMatcher(UriMatcher.NO_MATCH);
    		MATCHERtable1.addURI("(package).Provider", "table1", CONSTANTS);
    		MATCHERtable1.addURI("(package).Provider", "table1/#", CONSTANT_ID);
     
    		TABLE1_LIST_PROJECTION = new HashMap<String, String>();
    		TABLE1_LIST_PROJECTION.put(Provider.table1._ID, Provider.table1._ID);
    		TABLE1_LIST_PROJECTION.put(Provider.table1.MARICULE, Provider.table1.MATRICULE);
    		TABLE1_LIST_PROJECTION.put(Provider.table1.NOM, Provider.table1.NOM);
    		TABLE1_LIST_PROJECTION.put(Provider.table1.PRENOM, Provider.table1.PRENOM);
     
    	}
     
    	public static final class table12 implements BaseColumns {
    		public static final Uri CONTENT_URI
    				 =Uri.parse("content://(package)/table2");
     
    		public static final String DEFAULT_SORT_ORDER_MATRICULE = "Matricule";
    	    	public static final String _ID = "_id";		
    		public static final String MARICULE = "Matricule";
    	    	public static final String LOISIR = "Loisir";
    	    	public static final String ACTIVITE = "Activite";	    
    	}
     
    	static {
    		MATCHERtable2=new UriMatcher(UriMatcher.NO_MATCH);
    		MATCHERtable2.addURI("(package).Provider", "table2", CONSTANTS);
    		MATCHERtable2.addURI("(package).Provider", "table2/#", CONSTANT_ID);
     
    		TABLE2_LIST_PROJECTION = new HashMap<String, String>();
    		TABLE2_LIST_PROJECTION.put(Provider.table2._ID, Provider.table2._ID);
    		TABLE2_LIST_PROJECTION.put(Provider.table2.MARICULE, Provider.table2.MARICULE);
    		TABLE2_LIST_PROJECTION.put(Provider.table2.LOISIR, Provider.table2.LOISIR);
    		TABLE2_LIST_PROJECTION.put(Provider.table2.ACTIVITE, Provider.table2.ACTIVITE);
    	}	
     
     
    	public String getDbName() {
    		return(DATABASE_NAME);
    	}
     
    	public int getDbVersion() {
    		return(DATABASE_VERSION);
    	}
     
    	private class DatabaseHelper extends SQLiteOpenHelper {
    		public DatabaseHelper(Context context) {
    			super(context, DATABASE_NAME, null, DATABASE_VERSION);
    		}
     
    		@Override
    		public void onCreate(SQLiteDatabase db) {
     
    		}
     
    		@Override
    		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    			//
    		}
    	}
     
    	private SQLiteDatabase db;
     
    	@Override
    	public boolean onCreate() {
    		db=(new DatabaseHelper(getContext())).getReadableDatabase();
     
    		return (db == null) ? false : true;
    	}	
     
    	@Override
    	public Cursor query(Uri url, String[] projection, String selection,
     
    		String[] selectionArgs, String sort) {
    		SQLiteQueryBuilder qb=new SQLiteQueryBuilder();
     
    		qb.setTables(getTableName(url));
     
    		if (isCollectionUri(url)) {
    			qb.setProjectionMap(getDefaultProjection(url));
    		}
    		else {
    			qb.appendWhere(getIdColumnName()+"="+url.getPathSegments().get(1));
    		}
     
    		String orderBy;
     
    		if (TextUtils.isEmpty(sort)) {
    			orderBy=getDefaultSortOrder();
    		} else {
    			orderBy=sort;
    		}
     
    		Cursor c=qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
    		c.setNotificationUri(getContext().getContentResolver(), url);
    		return c;
    	}
     
    	@Override
    	public String getType(Uri url) {
    		if (isCollectionUri(url)) {
    			return(getCollectionType());
    		}
     
    		return(getSingleType());
    	}
     
     
    	private boolean isCollectionUri(Uri url) {
    		if (url.toString() == Provider.table1.CONTENT_URI.toString()) {
    			return(MATCHERtable1.match(url)==CONSTANTS);
    		}
    		else {
    			if (url.toString() == Provider.table2.CONTENT_URI.toString()) 
    				return(MATCHERtable12.match(url)==CONSTANTS);
    			else return false;
    		}
    	}
     
    	private HashMap<String, String> getDefaultProjection(Uri url) {
    		if (url.toString() == Provider.table1.CONTENT_URI.toString()) {
    			return(TABLE1_LIST_PROJECTION);
    		}
    		else {
    			if (url.toString() == Provider.table2.CONTENT_URI.toString()) 
    				return(TABLE2_LIST_PROJECTION);
    			else return(null);
    		}		
    	}
     
    	private String getTableName(Uri url) {
    		if (url.toString() == Provider.table1.CONTENT_URI.toString()) {
    			return(DATABASE_TABLE_TABLE1);
    		}
    		else {
    			if (url.toString() == Provider.table2.CONTENT_URI.toString()) 
    				return(DATABASE_TABLE_TABLE2);
    			else return("");
    		}
    	}
     
    }
    pour les functions

    getIdColumnName()
    getDefaultSortOrder()
    getCollectionType()
    getSingleType()

    pas de changement

Discussions similaires

  1. mysql_fetch_object avec plusieurs tables
    Par AlphaYoDa dans le forum SQL Procédural
    Réponses: 5
    Dernier message: 19/04/2007, 14h48
  2. Requête : Insert avec plusieurs tables
    Par bleu_ciel dans le forum Access
    Réponses: 4
    Dernier message: 01/06/2006, 21h42
  3. [VB 2005]Dataset avec plusieurs tables
    Par estelledany dans le forum Windows Forms
    Réponses: 3
    Dernier message: 29/05/2006, 14h25
  4. [MySQL] requete avec liaisons avec plusieurs tables
    Par pod1978 dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 10/03/2006, 22h21
  5. historique avec plusieurs table
    Par Cybher dans le forum Requêtes
    Réponses: 2
    Dernier message: 05/08/2005, 16h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo