Android
Data objects
Query di base su Android con Parse SDK: Guida completa
12 min
query di base su android introduzione in questa guida, eseguirai query di base in parse parse e implementerai un android android app utilizzando queste query imparerai come configurare e interrogare dati realistici utilizzando back4app back4app e android android questo tutorial utilizza un'app creata in android studio 4 1 1 con buildtoolsversion=30 0 2 buildtoolsversion=30 0 2 , compile sdk version = 30 0 2 compile sdk version = 30 0 2 e targetsdkversion=30 targetsdkversion=30 in qualsiasi momento, puoi accedere al progetto android completo costruito con questo tutorial nei nostri repository github repository di esempio kotlin repository di esempio java obiettivo il nostro obiettivo è interrogare i dati memorizzati su back4app back4app da un android android app ecco un'anteprima di ciò che realizzeremo prerequisiti per completare questo tutorial, abbiamo bisogno di android studio un'app creata su back4app nota segui il tutorial nuova app parse per imparare a creare un'app parse su back4app un'app android collegata a back4app nota segui il tutorial installare parse sdk per creare un progetto android studio collegato a back4app un dispositivo (o dispositivo virtuale ) che esegue android 4 1 (jelly bean) o versioni successive iniziamo! prima dei prossimi passi, dobbiamo connettere back4app back4app alla nostra applicazione dovresti salvare il appid appid e clientkey clientkey dal back4app back4app nel file string xml string xml e poi inizializza parse parse nel nostro app java app java o app kt app kt file segui il nuovo tutorial per parse app se non sai come inizializzare parse parse nella tua app oppure puoi scaricare i progetti di cui abbiamo condiviso i link github sopra e modificare solo le parti appid appid e clientkey clientkey secondo le tue esigenze 1 comprendere la classe parse query qualsiasi operazione di query parse utilizza il parsequery parsequery tipo di oggetto, che ti aiuterà a recuperare dati specifici dal tuo database all'interno della tua app è fondamentale sapere che un parsequery parsequery si risolverà solo dopo aver chiamato un metodo di recupero (come query findinbackground query findinbackground ), quindi una query può essere impostata e diversi modificatori possono essere concatenati prima di essere effettivamente chiamati per creare un nuovo parsequery parsequery , è necessario passare come parametro la sottoclasse parseobject desiderata, che è quella che conterrà i risultati della tua query un esempio di query può essere visto qui sotto, in cui viene interrogata una sottoclasse profile fittizia 1 // this will create your query 2 parsequery\<parseobject> query = new parsequery<>("profile"); 3 // the query will resolve only after calling this method 4 query findinbackground();1 // this will create your query 2 val query = parsequery\<parseobject>("profile") 3 // the query will resolve only after calling this method 4 query findinbackground() puoi leggere di più sulla parse query parse query classe qui nella documentazione ufficiale https //www back4app com/docs/javascript/parse javascript sdk 2 salva alcuni dati su back4app in questo passaggio, creeremo una classe con la console js e i codici javascript forniti da parse parse e creeremo query per questa classe creiamo una profilo profilo classe, che sarà l'obiettivo delle nostre query in questa guida su parse parse dashboard console javascript è possibile eseguire codice javascript direttamente, interrogando e aggiornando i contenuti del database della tua applicazione utilizzando i comandi js sdk esegui il codice qui sotto dalla tua console js e inserisci i dati su back4app ecco come appare la console js nel tuo dashboard procedi e crea l'utente profilo profilo classe con il seguente contenuto di esempio 1 // add profile objects and create table 2 // adam sandler 3 let profile = new parse object('profile'); 4 profile set('name', 'adam sandler'); 5 profile set('birthday', new date('09/09/1966')); 6 profile set('friendcount', 2); 7 profile set('favoritefoods', \['lobster', 'bread']); 8 await profile save(); 9 10 // adam levine 11 profile = new parse object('profile'); 12 profile set('name', 'adam levine'); 13 profile set('birthday', new date('03/18/1979')); 14 profile set('friendcount', 52); 15 profile set('favoritefoods', \['cake', 'bread']); 16 await profile save(); 17 18 // carson kressley 19 profile = new parse object('profile'); 20 profile set('name', 'carson kressley'); 21 profile set('birthday', new date('11/11/1969')); 22 profile set('friendcount', 12); 23 profile set('favoritefoods', \['fish', 'cookies']); 24 await profile save(); 25 26 // dan aykroyd 27 profile = new parse object('profile'); 28 profile set('name', 'dan aykroyd'); 29 profile set('birthday', new date('07/01/1952')); 30 profile set('friendcount', 66); 31 profile set('favoritefoods', \['jam', 'peanut butter']); 32 await profile save(); 33 34 // eddie murphy 35 profile = new parse object('profile'); 36 profile set('name', 'eddie murphy'); 37 profile set('birthday', new date('04/03/1961')); 38 profile set('friendcount', 49); 39 profile set('favoritefoods', \['lettuce', 'pepper']); 40 await profile save(); 41 42 // fergie 43 profile = new parse object('profile'); 44 profile set('name', 'fergie'); 45 profile set('birthday', new date('03/27/1975')); 46 profile set('friendcount', 55); 47 profile set('favoritefoods', \['lobster', 'shrimp']); 48 await profile save(); 49 50 console log('success!'); 3 interroga i dati ora che hai una classe popolata, possiamo ora eseguire alcune query di base in essa iniziamo filtrando profilo profilo i risultati per nome, che è un campo di tipo stringa, cercando valori che contengono il nome adam adam utilizzando il parse query contains parse query contains metodo 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 // `contains` is a basic query method that checks if string field 5 // contains a specific substring 6 parsequery contains('name', 'adam'); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(result get('name')); 16 }; ora facciamo una query per il campo di tipo numero friendcount friendcount utilizzando un altro metodo di query comune, parse query greaterthan parse query greaterthan in questo caso, vogliamo utenti profiles profiles in cui il conteggio degli amici è maggiore di 20 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 // `greaterthan` is a basic query method that does what it 5 // says on the tin 6 parsequery greaterthan('friendcount', 20); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(`name ${result get('name')}, friend count ${result get('friendcount')}`); 16 }; altri metodi di query ricorrenti sono parse query ascending parse query ascending e parse query descending parse query descending , responsabili dell'ordinamento delle tue query questo ordinamento può essere fatto nella maggior parte dei tipi di dati, quindi ordiniamo una query per il campo data birthday birthday dal più giovane 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 // `descending` and `ascending` can and should be chained 5 // with other query methods to improve your queries 6 parsequery descending('birthday'); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(`name ${result get('name')}, birthday ${result get('birthday')}`); 16 }; come affermato qui prima, puoi e dovresti concatenare i metodi di query per ottenere risultati più raffinati combiniamo quindi i precedenti esempi in una singola richiesta di query 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 parsequery contains('name', 'adam'); 5 parsequery greaterthan('friendcount', 20); 6 parsequery descending('birthday'); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(`name ${result get('name')}, friend count ${result get('friendcount')}, birthday ${result get('birthday')}`); 16 }; 4 query dalla nostra app android ora eseguiremo le operazioni che abbiamo fatto sopra dalla console js con java e kotlin nella nostra applicazione android elencheremo i profili facendo 4 query diverse 1 private void doquerybyname() { 2 parsequery\<parseobject> query = new parsequery<>("profile"); 3 query wherecontains("name", "adam"); 4 progressdialog show(); 5 query findinbackground((objects, e) > { 6 progressdialog hide(); 7 if (e == null) { 8 adapter = new resultadapter(this, objects); 9 resultlist setlayoutmanager(new linearlayoutmanager(this)); 10 resultlist setadapter(adapter); 11 } else { 12 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 13 } 14 }); 15 } 16 17 private void doquerybyfriendcount() { 18 parsequery\<parseobject> query = new parsequery<>("profile"); 19 query wheregreaterthan("friendcount", 20); 20 progressdialog show(); 21 query findinbackground((objects, e) > { 22 progressdialog hide(); 23 if (e == null) { 24 adapter = new resultadapter(this, objects); 25 resultlist setlayoutmanager(new linearlayoutmanager(this)); 26 resultlist setadapter(adapter); 27 } else { 28 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 29 } 30 }); 31 } 32 33 private void doquerybyordering() { 34 parsequery\<parseobject> query = new parsequery<>("profile"); 35 query orderbydescending("birthday"); 36 progressdialog show(); 37 query findinbackground((objects, e) > { 38 progressdialog hide(); 39 if (e == null) { 40 adapter = new resultadapter(this, objects); 41 resultlist setlayoutmanager(new linearlayoutmanager(this)); 42 resultlist setadapter(adapter); 43 } else { 44 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 45 } 46 }); 47 } 48 49 private void doquerybyall() { 50 parsequery\<parseobject> query = new parsequery<>("profile"); 51 query wherecontains("name", "adam"); 52 query wheregreaterthan("friendcount", 20); 53 query orderbydescending("birthday"); 54 progressdialog show(); 55 56 query findinbackground((objects, e) > { 57 progressdialog hide(); 58 59 if (e == null) { 60 adapter = new resultadapter(this, objects); 61 resultlist setlayoutmanager(new linearlayoutmanager(this)); 62 resultlist setadapter(adapter); 63 resultlist setnestedscrollingenabled(false); 64 } else { 65 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 66 } 67 }); 68 }1 private fun doquerybyname() { 2 val query = parsequery\<parseobject>("profile") 3 query wherecontains("name", "adam") 4 progressdialog!! show() 5 query findinbackground { objects list\<parseobject>?, e parseexception? > 6 progressdialog!! hide() 7 if (e == null) { 8 adapter = resultadapter(this, objects) 9 resultlist!! layoutmanager = linearlayoutmanager(this) 10 resultlist!! adapter = adapter 11 } else { 12 toast maketext(this, e localizedmessage, toast length short) show() 13 } 14 } 15 } 16 17 private fun doquerybyfriendcount() { 18 val query = parsequery\<parseobject>("profile") 19 query wheregreaterthan("friendcount", 20) 20 progressdialog!! show() 21 query findinbackground { objects list\<parseobject>?, e parseexception? > 22 progressdialog!! hide() 23 if (e == null) { 24 adapter = resultadapter(this, objects) 25 resultlist!! layoutmanager = linearlayoutmanager(this) 26 resultlist!! adapter = adapter 27 } else { 28 toast maketext(this, e localizedmessage, toast length short) show() 29 } 30 } 31 } 32 33 private fun doquerybyordering() { 34 val query = parsequery\<parseobject>("profile") 35 query orderbydescending("birthday") 36 progressdialog!! show() 37 query findinbackground { objects list\<parseobject>?, e parseexception? > 38 progressdialog!! hide() 39 if (e == null) { 40 adapter = resultadapter(this, objects) 41 resultlist!! layoutmanager = linearlayoutmanager(this) 42 resultlist!! adapter = adapter 43 } else { 44 toast maketext(this, e localizedmessage, toast length short) show() 45 } 46 } 47 } 48 49 private fun doquerybyall() { 50 val query = parsequery\<parseobject>("profile") 51 query wherecontains("name", "adam") 52 query wheregreaterthan("friendcount", 20) 53 query orderbydescending("birthday") 54 progressdialog!! show() 55 query findinbackground { objects list\<parseobject>?, e parseexception? > 56 progressdialog!! hide() 57 if (e == null) { 58 adapter = resultadapter(this, objects) 59 resultlist!! layoutmanager = linearlayoutmanager(this) 60 resultlist!! adapter = adapter 61 resultlist!! isnestedscrollingenabled = false 62 } else { 63 toast maketext(this, e localizedmessage, toast length short) show() 64 } 65 } 66 } è fatto! alla fine di questa guida, hai imparato come funzionano le query di base sui dati su parse e come eseguirle su back4app da un'app android