Android
Data objects
AndroidでのCRUD操作: Parseオブジェクトの基本的な実装方法
12 分
androidにおけるcrud parseオブジェクト はじめに このセクションでは、基本的なcrud操作を実行するandroidアプリケーションを構築します。crudはcreate read update deleteの略です。parseにデータを保存すると、parseobjectを中心に構築され、各オブジェクトにはjson互換のデータのキーと値のペアが含まれます。 back4appデータベースに保存できる値は、string、number、bool、array、object、date、file、pointer、relation、nullの型になります。 データ型についての詳細は、 こちら をクリックすることで確認できます。 このチュートリアルでは、 buildtoolsversion=30 0 2 buildtoolsversion=30 0 2 で作成された基本的なアプリを使用します。 compile sdk version = 30 0 2 compile sdk version = 30 0 2 と targetsdkversion 30 targetsdkversion 30 いつでも、私たちのgithubリポジトリを通じてプロジェクト全体にアクセスできます。 kotlinのサンプルリポジトリ javaのサンプルリポジトリ 目標 ここに、私たちが達成しようとしていることのプレビューがあります 前提条件 このチュートリアルを完了するには、次のものが必要です: android studio back4appで作成されたアプリ。 注意 新しいparseアプリのチュートリアル を参照して、back4appでparseアプリを作成する方法を学んでください。 back4appに接続されたandroidアプリ。 注意 parse sdkのインストールチュートリアル を参照して、back4appに接続されたandroid studioプロジェクトを作成してください。 android 4 1(jelly bean)以上を実行しているデバイス(または 仮想デバイス )。 私たちのtodoアプリを理解する androidでparseをよりよく理解するために、todoアプリで実装されたcrud操作を見ていきます。このアプリケーションは、タスクを登録するためのタイトルと説明のテキストフィールド、および登録されたタスクのリストを持つシンプルなインターフェースを持っています。各タスクのタイトルや説明を更新することができます。 注意 このチュートリアルでは、独自のカスタムアラートダイアログを作成します。したがって、コードはテンプレートに従うように構成されます。自分のデザインに合わせて調整できます。 問題が発生した場合は、次のgithubリポジトリを確認してください。 kotlin と java を確認してください。 始めましょう! 次のステップに従うことで、back4appデータベースにタスクを保存するtodoアプリを構築できるようになります。 1 todoアプリテンプレートを作成する android studioに移動し、 activity main xml activity main xml レイアウトファイルをプロジェクトから見つけて、 (project/app/res/layout/activity main xml) (project/app/res/layout/activity main xml) 以下のコードを自分のコードに置き換えます。このxmlコードは私たちの mainactivityの mainactivityの デザインとなり、これらのビューを私たちの mainactivity java mainactivity java クラスにバインドして使用します。 1 \<?xml version="1 0" encoding="utf 8"?> 2 \<androidx constraintlayout widget constraintlayout xmlns\ android="http //schemas android com/apk/res/android" 3 xmlns\ app="http //schemas android com/apk/res auto" 4 xmlns\ tools="http //schemas android com/tools" 5 android\ layout width="match parent" 6 android\ layout height="match parent" 7 tools\ context=" mainactivity"> 8 9 \<! title >> 10 \<textview 11 android\ id="@+id/textview" 12 android\ layout width="0dp" 13 android\ layout height="wrap content" 14 android\ background="@color/blue 700" 15 android\ gravity="center horizontal" 16 android\ padding="24dp" 17 android\ text="todo list" 18 android\ textcolor="@color/white" 19 app\ layout constraintend toendof="parent" 20 app\ layout constraintstart tostartof="parent" 21 app\ layout constrainttop totopof="parent" /> 22 23 \<! we will open a pop up view when clicked to this button >> 24 \<com google android material floatingactionbutton floatingactionbutton 25 android\ id="@+id/fab" 26 style="@style/widget materialcomponents floatingactionbutton" 27 android\ layout width="wrap content" 28 android\ layout height="wrap content" 29 android\ layout margin="16dp" 30 android\ scaletype="centerinside" 31 android\ src="@drawable/ic baseline add 24" 32 app\ backgroundtint="@color/blue 700" 33 app\ fabsize="normal" 34 app\ layout constraintbottom tobottomof="parent" 35 app\ layout constraintend toendof="parent" 36 app\ tint="@color/white" /> 37 38 \<! we will show this text view when data list is empty >> 39 \<textview 40 android\ id="@+id/empty text" 41 android\ layout width="wrap content" 42 android\ layout height="wrap content" 43 android\ text="list is empty" 44 android\ layout margintop="32dp" 45 android\ textsize="20sp" 46 android\ visibility="gone" 47 app\ layout constraintend toendof="parent" 48 app\ layout constraintstart tostartof="parent" 49 app\ layout constrainttop tobottomof="@+id/textview" /> 50 51 \<! we will adapt the data list to this recyclerview > 52 \<androidx recyclerview\ widget recyclerview 53 android\ id="@+id/recyclerview" 54 android\ layout width="0dp" 55 android\ layout height="0dp" 56 app\ layout constraintbottom tobottomof="parent" 57 app\ layout constraintend toendof="parent" 58 app\ layout constraintstart tostartof="parent" 59 app\ layout constrainttop tobottomof="@+id/textview"> 60 \</androidx recyclerview\ widget recyclerview> 61 \</androidx constraintlayout widget constraintlayout> 2 オブジェクトを作成する create関数は、タイトルと説明を持つ新しいタスクを作成します。 todoを追加するには、画面に表示されるポップアップにタイトルと説明の値を入力し、それをparseobjectに設定してこのオブジェクトを保存します。このオブジェクトは、parseが提供している関数を使用してデータベースに保存します。 注意 このプロジェクトでは、自分自身のカスタムアラートダイアログを作成します。アラートダイアログを好きなようにデザインし、後で使用するダイアログのビューに設定できます。 1 openinputpopupdialogbutton setonclicklistener(fabbuttonview > { 2 alertdialog builder alertdialogbuilder = new alertdialog builder(mainactivity this); 3 alertdialogbuilder settitle("create a todo"); 4 alertdialogbuilder setcancelable(true); 5 initpopupviewcontrols(); 6 //we are setting our custom popup view by alertdialog builder 7 alertdialogbuilder setview(popupinputdialogview); 8 final alertdialog alertdialog = alertdialogbuilder create(); 9 alertdialog show(); 10 savetodobutton setonclicklistener(savebuttonview > savetodo(alertdialog)); 11 canceluserdatabutton setonclicklistener(cancelbuttonview > alertdialog cancel()); 12 }); 13 14 //this is our savetodo function 15 private void savetodo(alertdialog alertdialog) { 16 parseobject todo = new parseobject("todo"); 17 if (titleinput gettext() tostring() length() != 0 && descriptioninput gettext() tostring() length() != 0) { 18 alertdialog cancel(); 19 progressdialog show(); 20 todo put("title", titleinput gettext() tostring()); 21 todo put("description", descriptioninput gettext() tostring()); 22 todo saveinbackground(e > { 23 progressdialog dismiss(); 24 if (e == null) { 25 //we saved the object and fetching data again 26 gettodolist(); 27 } else { 28 //we have an error we are showing error message here 29 showalert("error", e getmessage()); 30 } 31 }); 32 } else { 33 showalert("error", "please enter a title and description"); 34 } 35 }1 openinputpopupdialogbutton? setonclicklistener { fabbuttonview > 2 val alertdialogbuilder = alertdialog builder(this\@mainactivity) 3 alertdialogbuilder settitle("create a todo") 4 alertdialogbuilder setcancelable(true) 5 initpopupviewcontrols() 6 //we are setting our custom popup view by alertdialog builder 7 alertdialogbuilder setview(popupinputdialogview) 8 val alertdialog = alertdialogbuilder create() 9 alertdialog show() 10 savetodobutton? setonclicklistener { savebuttonview > 11 savedata(alertdialog) 12 } 13 canceluserdatabutton? setonclicklistener { cancelbuttonview > 14 alertdialog cancel() 15 } 16 } 17 18 //this is our savetodo function 19 private fun savedata(alertdialog alertdialog) { 20 val todo = parseobject("todo") 21 if (titleinput? text tostring() isnotempty() && descriptioninput? text tostring() isnotempty()) { 22 alertdialog cancel() 23 progressdialog? show() 24 todo put("title", titleinput? text tostring()) 25 todo put("description", descriptioninput? text tostring()) 26 todo saveinbackground { e > 27 progressdialog? dismiss() 28 if (e == null) { 29 //we saved the object and fetching data again 30 gettodolist() 31 } else { 32 //we have an error we are showing error message here 33 showalert("error", e message!!) 34 } 35 } 36 } else { 37 showalert("error", "please enter a title and description") 38 } 39 } 3 オブジェクトを読む parseが提供してくれた関数を使って、クラス内のすべてのデータをparseobjectリストとして取得できます。以下のようなクエリを作成し、todoクラス内のすべてのデータを取得します。 1 private void gettodolist() { 2 progressdialog show(); 3 parsequery\<parseobject> query = parsequery getquery("todo"); 4 query orderbydescending("createdat"); 5 query findinbackground((objects, e) > { 6 progressdialog dismiss(); 7 if (e == null) { 8 //we are initializing todo object list to our adapter 9 inittodolist(objects); 10 } else { 11 showalert("error", e getmessage()); 12 } 13 }); 14 }1 private fun gettodolist() { 2 progressdialog? show() 3 val query = parsequery getquery\<parseobject>("todo") 4 query orderbydescending("createdat") 5 query findinbackground { objects, e > 6 progressdialog? dismiss() 7 if (e == null) { 8 //we are initializing todo object list to our adapter 9 inittodolist(objects) 10 } else { 11 showalert("error", e message!!) 12 } 13 } 14 } この関数では、返されたリストをパラメータとして私たちの アダプター アダプター , この アダプター アダプター , を私たちの recyclerview recyclerview , に設定し、このリストの各オブジェクトの値をそれぞれのビューに印刷します。そして、リストにアイテムがない場合、私たちの empty text empty text , ビューを visible visible に設定します。 1 private void inittodolist(list\<parseobject> list) { 2 if (list == null || list isempty()) { 3 empty text setvisibility(view\ visible); 4 return; 5 } 6 empty text setvisibility(view\ gone); 7 8 todoadapter adapter = new todoadapter(list, this); 9 10 recyclerview\ setlayoutmanager(new linearlayoutmanager(this)); 11 recyclerview\ setadapter(adapter); 12 }1 private fun inittodolist(list list\<parseobject>?) { 2 if (list == null || list isempty()) { 3 empty text!! visibility = view\ visible 4 return 5 } 6 empty text? visibility = view\ gone 7 8 val adapter = todoadapter(list as arraylist\<parseobject>, this) 9 10 recyclerview? layoutmanager = linearlayoutmanager(this) 11 recyclerview? adapter = adapter 12 } 4 オブジェクトの更新 アダプターのビューにある編集ボタンを使用して、更新を行います。 mutablelivedata mutablelivedata , androidが提供するオブジェクトを使用して、ホームページの編集ボタンのクリックイベントをリッスンできます。このボタンがクリックされると、同じポップアップを開き、今回はクリックしたオブジェクトのタイトルと説明の値でこのポップアップを埋めます。そして、ユーザーがこの説明とタイトルを変更し、保存を押すと、最初にこのオブジェクトのidでデータベースから取得し、新しい変数を設定してオブジェクトを再度保存します。 注意 mutablelivedataはlivedataのサブクラスであり、そのプロパティ(setvalue/postvalue)に使用され、これらのプロパティを使用することでuiに簡単に通知できます。 1 adapter oneditlistener observe(this, parseobject > { 2 alertdialog builder alertdialogbuilder = new alertdialog builder(mainactivity this); 3 alertdialogbuilder settitle("update a todo"); 4 alertdialogbuilder setcancelable(true); 5 //we are initializing popup views with title and description parameters of parseobject 6 initpopupviewcontrols(parseobject getstring("title"), parseobject getstring("description")); 7 alertdialogbuilder setview(popupinputdialogview); 8 final alertdialog alertdialog = alertdialogbuilder create(); 9 alertdialog show(); 10 savetodobutton setonclicklistener(savetodobuttonview > { 11 if (titleinput gettext() tostring() length() != 0 && descriptioninput gettext() tostring() length() != 0) { 12 alertdialog cancel(); 13 progressdialog show(); 14 parseobject put("title", titleinput gettext() tostring()); 15 parseobject put("description", descriptioninput gettext() tostring()); 16 parseobject saveinbackground(e1 > { 17 progressdialog dismiss(); 18 if (e1 == null) { 19 gettodolist(); 20 } else { 21 showalert("error", e1 getmessage()); 22 } 23 }); 24 } else { 25 showalert("error", "please enter a title and description"); 26 } 27 }); 28 canceluserdatabutton setonclicklistener(cancelbuttonview > alertdialog cancel()); 29 });1 adapter clicklistenertoedit observe(this\@mainactivity, { parseobject > 2 val alertdialogbuilder = alertdialog builder(this\@mainactivity) 3 alertdialogbuilder settitle("update a todo") 4 alertdialogbuilder setcancelable(true) 5 6 //we are initializing popup views with title and description parameters of parseobject 7 8 initpopupviewcontrols( 9 parseobject getstring("title")!!, 10 parseobject getstring("description")!! 11 ) 12 13 alertdialogbuilder setview(popupinputdialogview) 14 val alertdialog = alertdialogbuilder create() 15 alertdialog show() 16 17 savetodobutton? setonclicklistener { savebuttonview > 18 if (titleinput? text tostring() isnotempty() && descriptioninput? text tostring() isnotempty()) { 19 alertdialog cancel() 20 progressdialog? show() 21 parseobject put("title", titleinput? text tostring()) 22 parseobject put("description", descriptioninput? text tostring()) 23 parseobject saveinbackground { e1 > 24 progressdialog? dismiss() 25 if (e1 == null) { 26 gettodolist() 27 } else { 28 showalert("error", e1 message!!) 29 } 30 } 31 } else { 32 showalert("error", "please enter a title and description") 33 } 34 } 35 }) 5 オブジェクトを削除 私たちは、アダプターのビューで削除ボタンのクリックイベントを、 mutablelivedata mutablelivedata オブジェクトをホームページから取得し、編集ボタンと同様に処理します。削除ボタンがクリックされると、parseobjectのオブジェクトidを削除関数にパラメータとして渡し、このオブジェクトをデータベースから削除します。 1 adapter ondeletelistener observe(this, parseobject > { 2 progressdialog show(); 3 parseobject deleteinbackground(e > { 4 progressdialog dismiss(); 5 if (e == null) { 6 //we deleted the object and fetching data again 7 gettodolist(); 8 } else { 9 showalert("error",e getmessage()); 10 } 11 }); 12 });1 adapter ondeletelistener observe(this\@mainactivity, { parseobject > 2 progressdialog? show() 3 parseobject deleteinbackground { e > 4 progressdialog? dismiss() 5 if (e == null) { 6 //we deleted the object and fetching data again 7 gettodolist() 8 } else { 9 showalert("error", e message!!) 10 } 11 } 12 }) 完了しました! この時点で、android上でparseを使用して基本的なcrud操作を行う方法を学びました。