MVC Pattern Shopping Cart Android_Examp le In this example showing use of MVC Pattern to create a very asic Shopping cart! Creating very asic Controller Class and defined in application context and you can use this Control ler Class as a "loal class! If any View#Activit y$ re%uired values then view should call controller and then controller will call appropriate Model and provide value to view! Models can also communicate with each others to exchange values! http&''androidexample!com'(se_MVC_Pattern_)o_Create_Very_*asic_Shoppin g_Cart__+ _Android_Example'index!php, view-article_discription.aid-//0.aaid-/12 n this example showing use of MVC Pattern to create a very asic Shopping cart! Creating very asic Controller Class and defined in application context and you can use this Controller Class as a "loal class! If any View#Activity$ re%uired values then view should call controller and then controller will call appropriate Model and provide value to view! Models can also communicate with each others to exchange values!
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
In this example showing use of MVC Pattern to create a very asic Shoppingcart! Creating very asic Controller Class and defined in application context andyou can use this Controller Class as a "loal class! If any View#Activity$ re%uiredvalues then view should call controller and then controller will call appropriateModel and provide value to view!
Models can also communicate with each others to exchange values!
n this example showing use of MVC Pattern to create a very asic Shopping cart!Creating very asic Controller Class and defined in application context and youcan use this Controller Class as a "loal class! If any View#Activity$ re%uiredvalues then view should call controller and then controller will call appropriateModel and provide value to view!
Models can also communicate with each others to exchange values!
Create a ModelProducts type Arraylist to store ModelProducts instances!
File : src/Cotroller.java Controller class extends with android!app!Application and defined in the application tag in your
AndroidManifest!xml file! Android will create an instance of Controller class and ma3e it availalefor your entire application context! 4ou can get o5ect of your class on any activity ' roadcastreceiver ' service in application context#environment$ y Context!getApplicationContext#$ method!
Controller class to intract with models and provide values to views!
Create a ModelProducts type Arraylist to store ModelProducts instances!Create a ModelCart type refference to store ModelCart instance to intract with ModelCart!
File : !droidMai"est.xml
Assign Controller!5ava in application tag6 see this line in applicationtag adroid:ame#$com.adroidexample.mvc.Cotroller$!"ter assi% &ou ca %et Cotroller.java istace o a& activit& i applicatio cotext.
File : src/FirstScree.java %et!pplicatioCotext'( method o" Cotext will %ive Cotroller.java o)ject* use this o)jectto store product details ad the store o)ject i !rra&list.
Creati% view elemets d&amicall& to show product details ad add to cart )uttos.
!"ter click add to cart )utto product o)ject will store i !rra&list de"ied i ModelCartclass.
File : src/SecodScree.java Showi% ModelProduct o)jects details o scree that stored i ModelCart !rra&list.
File : src/ThirdScree.java Showi% ModelProduct o)jects details o scree that stored i ModelCart !rra&list.
+commi% ,ut%oi% Call -o% With Call uratio !droid Project
n )his pro5ect creating a incomming outgoing call log with call duration!
WorkFlow :Created three roadcast reciever! a. *roadcast 7eciever for Incomming call #IncomingCall!5ava$! ). *roadcast 7eciever for 8utgoing call #8utgoingCall!5ava$! c. *roadcast 7eciever for Call duration #Call9uration7eceiver!5ava$!Created controller file Controller!5avaCreated a list to show what type of call log want to show! created three files a. Call:ist!5ava to show call list options! ). Call:ist9ata!5ava to create each row data in list! c. Call:istValues!5ava Model to store each row data for list!9*Adapter!5ava to store call data in s%lite dataase!Call9etailServer!5ava to store s%lite data to server and show call data to activity!
Flow :
0. ;hen a call will come then IncommingCall!5ava and Call9uration7eceiver!5ava roadcastrecievers called and data store in s%lite dataase! 1. ;hen a call will ma3e to other then 8utgoingCall!5ava and Call9uration7eceiver!5avaroadcast recievers called and data store in s%lite dataase! 2. ;hen clic3 on Call log type #today'wee3ly'monthly$ one re%uest sent to server andcalllogserver!php called and return result ased on imei numer and show on activity!
owload +ma%es From We) !d -a3& -oad +-ist4iew !droid Example
n this example downloading images from we to a listview! (sing la<y loading to downloadimages in a listview! (sing custom adapter to create lisview rows and using Image:oader class tola<y load images from we and show in listview row! Clic3 on listview showing image url in alert!
Steps : Mai!ctivit&.java : Initiali<e static image url in string array and create listview! -a3&+ma%e-oad!dapter.java : (sed to create each list row! Inflate taitem!xml file for eachrow and call Image:oader!5ava to download image from url and resi<e downloaded image cache
on sdcard! +ma%e-oader.java : (sed to download image from url and resi<e downloaded image andma3e file cache on sdcard! :a<y load images for listview rows! FileCache.java : (sed to create folder at sdcard and create map to store downloaded imageinformation! Memor&Cache.java : (sed to set cache folder si<e limit # =ow much m'3 downloadedimage cache folder will store $ and also used to clear cache files from sdcard! 5tils.java : (sed to Create Cache image for images downloaded from we!
File : Mai!ctivit&.java + 9efine numer of image urls in a string array!
+ Call adapter class to create listview rows!
File : -a3&+ma%e-oad!dapter.java + Create custom adapter!+ Inflate taitem!xml file to create rows in listview!+ Call Image:oader!5ava to download and cache images from we!
File : File : listview6row.xml
File : +ma%e-oader.java + (sed to download images from we and resi<e!+ :a<y load images in listview!+ @urther explanation See in comments!
evice To evice Messa%i% 5si% 7oo%le CloudMessa%i% 7CM !droid Exa
n this example sending device to device #Moile )o Moile$ messaging using google cloudmessaging #"CM$! it is the very asic example you can use this to create advance levelmessaging application! (sing php as server side language and mys%l dataase 6 you can useother server side language #5sp'asp!net'asp$ and dataase #s%lserver'oracle$!
8,TE :Students can also use this pro5ect for semester pro5ect! )hey can modify and inhance examplesource code! =ere i am using =))P ased device to device messaging ut 7ight way to achieve the device todevice messaging is to implement "CM Cloud Connection Server #CCS$ ! "CM CloudConnection Server #CCS$ is a connection server ased on >MPP! CCS allows 1rd+party app
servers to communicate with Android devices y estalishing a persistent )CP connection with"oogle servers using the >MPP protocol! )his communication is asynchronous andidirectional#device to device$! Check this 59- "or MPP )ased 7CM Cloud Coectio Server 'CCS(:http&''developer!android!com'google'gcm'ccs!html this url is using Smac3 API for AVA :ater i will give example to send device to device message with CCS and >MPP! *ut now going with this =))P ased messaging#chatting$! +" &ou are ew i 7oo%le Cloud Messa%i% '7CM( implemetatio the must )e read "irstthis example.
Android Push ?otifications using "oogle Cloud Messaging #"CM$ + PA7) / + this example usi% custom %rid la&out so also read this example.Custom "rid :ayout + Android Example + this example usi% custom spier so also read this example.Custom Spinner ;ith Image And )ext + Android Example
0. Edit configuration files and create support files!
1. Chec3 device is registered for weserver or not!
1!. if device is not registered then send to registration screen and after gcm registration save"CM regI9 and device IMEI to mys%l dataase on server 6 After registration redirect to show allregistered devices screen!
1;. if device is registered then get this device regI9 and IMEI from server and store it inSB:ite dataase on phone and redirect to show all registered devices screen!
2. Select user to send message and after selection show send message screen!
<. ;hen user sending message then send sender IMEI 6 sendto IMEI and message to weserver 6 weserver get user device data #regI9$ from mys%l dataase and call gcm to sendmessage to regI9!
=. ;hen "CM Client on device get new message store it in SB:ite dataase on phone andshow new notification message in notification ar!
>. After clic3ing notification showmessage screen will open and show all got messages and onshowmessage screen user can also send message to other devices #See output video$!
8,TE :
In this example i am showing 5ava'xml'php files step y step!
STEP0 : Edit co"i%uratio "iles ad createsupport "iles.
M- F+-E : !droidMai"est.xml )he following permission are re%uired to ma3e your pro5ect support gcm!
+8TE98ET )o ma3e your app use internet services!!CCESS68ETW,9@6ST!TE )o access networ3 state #used to detect internet status$7ET6!CC,58TS 7e%uired as "CM needs google accountW!@E6-,C@ ?eeded if your app need to wa3e your device when it sleeps
4+;9!TE ?eeded if your support viration when receiving notification Also add some roadcast receivers as mentioned elow!
In this Example (sing two configuration files!
Config!5ava for client side and config!php for server side!(sing three support files for clients Controller!5ava6 9*Adapter!5ava6 (ser9ata!5ava
A!4! F+-E : Co"i%.java )his is the main client side configuration file!
A!4! F+-E : 5serata.java )his is the model class to show user data!
A!4! F+-E : ;!dapter.java S%lite dataase helper class that defined user and message specific funtions!)his class contains funtions to insert'update'delete user data!
A!4! F+-E : Cotroller.java Controller!5ava is very important class 6 its contains all the client side function responsile for userdata flow!
Controller class extends with android!app!Application and defined in the application tag in your AndroidManifest!xml file! Android will create an instance of Controller class and ma3e it availalefor your entire application context! 4ou can get o5ect of your class on any activity ' roadcastreceiver ' service in application context#environment$ y calling Context!getApplicationContext#$method!
PP F+-E : "uctio.php Contains all functions responsile for gcm registration 6 fetch'insert'delete user data to mys%ldataase!
PP F+-E: loader.php Include configuration and function file and ma3e dataase connection! loader!php is centrali<edfile and it will include y other php file!
STEP 1 : Check device is re%istered "or We)serveror ot.
A!4! F+-E : Mai.java
PP F+-E : validate6device.php )his will validate user from mys%l dataase y IMEI numer posted from Main!5avaif IMEI numer found then send user data as S8? to device with status DupdateD!if IMEI numer not found then send user data as S8? to device with status DinstallD!
1!. if device is not registered then send to registration screen and after gcm registration store"CM regI9 and device IMEI to mys%l dataase on server 6 After registration redirect to show all
registered devices screen!
A!4! F+-E : 9e%ister!ctivit&.java 7egistration screen layout 6 fill name and mail id and send to MainActivity!5ava for "CMregistration!
7egister device for "oogle Cloud Messaging#"CM$ after successfull gcm registartion sendinggcm regI9 to server and store regI9 6name6IMEI in mys%l dataase for further use to sendmessages from gcm!
MainActivity!5ava file is using Controller!5ava methods for registration#see controller classregister#$ and post#$ method$!
Creating a roadcast receiver to get gcm message and show on screen#MainActivity!5ava$!
Mai!ctivit&.java "ile is usi% re%ister.php "ile to post 7CM ad device data to server.
PP F+-E : re%ister.php )his file will get post data and store it in mys%l dataase!Send a registration confirm message to device!
M- F+-E : activit&6mai.xml (sed for MainActivity!5ava file layout!
gistered devices screen!
A!4! F+-E : 7rid4iewExample.java )his file is showing all registered user in a Custom grid view!
Calling userdata!php file from server and get all user data as S8? and show data in a customgridview #see custom grid view example$!
After grid selection redirect to ShowMessage!5ava file with selected user data! pac9age co:;androide8a:ple;:obilegc:! i:port 6ava;io;B+ffered$eader! i:port 6ava;io;-np+tStrea:$eader! i:port 6ava;io;O+tp+tStrea:riter! i:port 6ava;net;$0! i:port 6ava;net;$0onnection! i:port 6ava;net;$0#ncoder!
/************ a9e &ost all To eb Server ***********/
B+ffered$eader reader@n+ll! String ontent @ ""! // Send data
tr
// Defined $0 >here to send data
$0 +rl @ ne> $0(para:sJGK!
// Set $e+est para:eter if(Lpara:sJ4K;e+als("" data C@"M" C $0#ncoder;encode("data", "TFN3" C if(Lpara:sJK;e+als("" data C@"M" C $0#ncoder;encode("data", "TFN3" C if(Lpara:sJPK;e+als("" data C@"M" C $0#ncoder;encode("dataP", "TFN3" C 0og;i(")",data!
M- F+-E : %rid6item.xml xml file used in Custom"ridAdapter!5ava file! ?8:l version@"4;G" encoding@"+tfN3"?E 0inear0ao+t 8:lnsIandroid@"httpI//sche:as;android;co:/ap9/res/android" androidIlao+t_>idth@">rap_content" androidIlao+t_height@">rap_content" androidIpadding@"dp" androidIorientation@"vertical" E
-:age5ie>
androidIid@"ACid/grid_ite:_i:age" androidIlao+t_>idth@"Gp8" androidIlao+t_height@"Gp8" androidIlao+t_:argin$ight@"4Gp8" androidIsrc@"Adra>able/+ser_th+:b" E /-:age5ie>E
STEP<. Whe user sedi% messa%e the sedseder +ME+ * sedto +ME+ ad messa%e to we)server * we)server %et user device data 're%+("rom m&sHl data)ase ad call %cm to sedmessa%e to re%+.
A!4! F+-E : SedPush8oti"icatio.java "et user values got from "ridViewExample!5ava file!