Android Base CRUD Final Project

My First Store App App Story Board SQL Persistence CRUD Products Video Full My First Store Example Download Full CRUD Example

 

What Cover The Final Base Project?

The final project is an example about how design and build an app with login, menu bar, CRUD Data managed with SQLite.

It’s included intelligent filters on edit text and navigation between activities.

My First Store Amazon-Appstore

My First Store Amazon-Appstore

My First Store App

Functionality resolve an Online Store, with:

  • User Register & Login.
  • Roles Managed like common User (an client) or Admin.
    • Admin Role let us:
      • List users.
      • List, Create or Delete Products on the store.
    • User Role let us:
      • Filter Products.
      • Adquiere Products, adding to Cart.
      • Query products details.
      • List and filter Purchases.

 

App Story Board

Following the views and principals transitions. We have two principal sections. One for normal user and other for admin user.

We are going to explain the te views and principal actions, then during the article detailed the different behaviors.

 

My First Store Story Board – Register & Login

The first step is register and user, you can create it from login men. Once registered we can proceed to access with the user. Currently the role is assigned internally. We can only create managed users from the data objects that we will explain later.

We can watch how the validations of the inputs and check of the previous user existence is executed. When all is right a dialog information about successful login is showed. In function of the role access to Products List or to Admin section.

 

My First Store Story Board – Product List

Here we are showing how works the Product List, this is the place where we are filter and buying the products. Pay attention to the filter product text, is a great help for looking for products. Each push the “ADD To CART”  Button the product is added to cart (obvious), in the next view “SHOW CART” we can review the summary of our purchase.

 

keikis Academy My First Store Story Board - Product List

keikis Academy My First Store Story Board – Product List

 

My First Store Story Board – Purchase List

Once finish the select our products we can review the cart. Filter by Product Name is enabled. Also a special layout is implemented for landscape orientation. Great!. The PAY is not implemented, it’s out of this course.

keikis Academy My First Store Story Board - Purchase List

keikis Academy My First Store Story Board – Purchase List

My First Store Story Board – Admin Menu

As we said, there is two roles, normal user and Admin user. When sign in with a Admin user an managed menu is showed. Three actions can be executed, list users, list, delete and create products in the store and review the purchases.

keikis Academy My First Store Story Board - Admin Menu

keikis Academy My First Store Story Board – Admin Menu

 

My First Store Story Board – Admin Product List

The Admin Product List let us filter products by name, delete one selected when a long touch is realized and introduce a new product. Before a new product is created a validation of mandatory fields and check of existence is necessary. If all was right a new product is inserted into de local data base.

keikis Academy My First Store Story Board - Admin Product List

keikis Academy My First Store Story Board – Admin Product List

 

My First Store Story Board – Admin Purchase List

In this case filter the purchases of all clients is the unique options. Pay attention to the possibility of filter an user by the spinner and also by text, besides we can apply adicional filter by product name. Three filters in action. Great!.

keikis Academy My First Store Story Board - Admin Purchase List

keikis Academy My First Store Story Board – Admin Purchase List

 

MVC Build Design

All project follow the Model View Controller paradigm which is  something inherent to Android development as we explain in the previous initial article.. Android Base Activity. The main pieces are Layouts as Views to paint results and request data, Activities like Controllers for the validations, navigation and integration with the model area. In this case the Model is built directly with Persistence Adapter by entities (perhaps no the more ortodoxo, but simple for our example level)   that we are going to study following.

 

My FirstStore Implementation Details – CRUD

Project Structure

 

Packages for Activities:

The activites has the next tasks.

  • LoginActivity: Let log in intro the app and also register a new user.
  • MainActivity: Principal activity shows menu bar and back image. Menu bar navigate to LoginActivity.
  • ListUserProductActivity: List product for the normal user (no admin), to let him add items to the cart and then finish with the purchase.
  • AdminMenuActivity: When the user is roled as Admin, this menu with three options (user, products & purchases) is showed.
  • CreateProductActivity: As admin you can create a new product.
  • ListProductActivity: As admin you can list product & delete.
  • ListPurchaseActivity: As admin also can review the purchases of all users in the store.
  • ListUserActivity: Finally an Admin user can manage the all list of users.

Packages for Database Adaptador: Let easily create, update, delete and access to app data.

Folder for layouts, we have one for landscape orientation when make a purchase review with the cart.

Folder values for string translations resources

AndroidStudio-MyFirstStore-Product-info

AndroidStudio-MyFirstStore-Product-info

 

Persistence

From Persistence Article Android Base Data Persistence  we have learn to work with databases Android system, let’s remember how deliver a SQLite inside app (embedded) engine. To make it easy also exists an Android framework (SQLiteOpenHelper) that help us to create the database and the tables. The first time we create our DataBaseHelper (this class extends SQLiteOpenHelper) the onCreate method is executed automatically creating the User table.

For the User entity (table), an adapter has been built UserDBAdapter, this object encapsulate the dialog with the database for resolve the create, read (select), insert and delete sentences.

 

This tradicional way to insert the default data is use the DB Adapters and in oncreate lifecycle event of the particular activiy (e.g ListUserProductActivity is managed users) we open, delete and insert the default information. Pay attention that is only a mechanism for the default data, like a demo app. In a production app you should have an initialization of data in the app start life-cycle and reset option available.

And then the example of how load the data by a fetch operation and using a SimpleCursorAdapter. 

Query Vs. RawQuery Method

Relative to Select data operation we are working with two different alternatives.

Using query method from Android

this is like all params necesary are passed to the function fro build the cursor versus the other way that is Raw query where diectly use a SQL Stament to dialog with the database.

Raw query is managed by the rawQuery method.

 

What’s An Adapter?

when we work with visual components like listviews the data and the view in Android are separated. The view is a layout only with the information about an item in the list, and the data is obtained from an external system or another place. The adapters are in charge of putting together both things and thus completing all the elements of a list.

With a SimpleCursorAdapter (is an adapter pre-build by Android) we can easily associate a cursor of data with a layout info (R.layout.product_info) , we need set the columns name of the table and the ids of the layout and then using the returned adapter populate de listview that will be displayed on the screen.

Don´t worry we are going to repeat with more detail in each case of use next.

How Check the SQL Data?

The SQL Data is stored on a db file into the app, an easy way to access is with the Android Tool adb, remember that the binary has to be on the system path and that we need be a root user to have permissions to access the directories and files.

The folder is databases and contains the User file (db), inside we can find the User table and its content, let’s see:

For more information watch the video Video User Activity on My Store.

Persistence Classes Structure

In our project we have a general class DataBaseHelper to help with the creates and database lyfecicle as we said, and also for each entity an Adapter to work with the CRUD. One por Product, Purchase & User

  • DataBaseHelper
  • ProductDBAdapter
  • PurchaseDBAdapter
  • UserDBAdapter

Also we have implemented a Custom CursorAdapter that is necessary to populate the Product List with button that onClick event can manage the current product on the item list. We are going to see this in details in the Product List explanations next.

  • customcursoradapter package
    ProductCustomCursorAdapter

 

Menu

The app start in a main activity that included a menu bar. This menu is build by the following code:

The menu bar has a menu.xml resource with the items showed by a star icon and a second item with textual “Login” title. ifRoom option tell to component that if there is space in the bar menu “Login” will be showed like a button and not like a left menu option.

Both let us navigate to the Login Activity:

AndroidStudio-MyFirstStore-Admin-Menu

AndroidStudio-MyFirstStore-Admin-Menu

MyFirstStore-AdminMenuActivity

MyFirstStore-AdminMenuActivity

Register & Login

As we comment from the menu bar we can access the login activity and from there we can register new users (not administrators) or login. 

In this case we are preparing the default data of the app, clean an insert some users. And also at this first moment are deleting de before purchases.

 

The default user inserted are:

 

 

Login validation

Both the registration and the access is valid both fields are filled in, in the case of registration also validate the existence or not of the user to register. This is solved by performing a search in the database and verifying its existence, if it exists the error would be reported.

 

An important detail, when the validations are passed correctly we store the user’s information in a global object to the app, to be able to query later in the rest of activities. E.g. filter the role to know if show the Admin menu, or filter Purchases for a current logged user. 

 

 

The final actions are controlled by Dialog, when all was fine in the case of login we are starting the ListProductActivity and when registering call a finishRegister which insert into Data Base the new user.

 

 

 

User Interaction

 List Products

Resolved by created a ProductDBAdapter, clean and insert the default products. To display create a SimpleCursorAdapter with the mapping of cursor of all products (Cursor cursor = dbProductAdapter.fetchAllProducts()) and a view item R.layout.product_info. The SimpleCursorAdapter is then assigned to a Listview of the ListProductActivity to show all records.

 

The item Product Layout

This is only the layout of the item that describe a product, each item is inserted in the Listview1 by the adapter.

AndroidStudio-MyFirstStore-Product-info

AndroidStudio-MyFirstStore-Product-info

Filter Products

Filter products is a easy task. First we have created an editText on  called myFilter. On this component is added an TextChangedListener to detect when the text change. Add the filter is made over the dataAdapter calling to filter method.

To get the filter to work correctly we have set a filter to the dataAdapter with the right parameters, that is indicated again with setFilterQueryProvider. We can see how we create a new FilterQueryProvider, indicated that the query to execute is dbProductAdapter.fetchProductsByName (constraint.toString ()) ;.

Pay attention, the charSequence (s) passed to dataAdapter.getFilter().filter(s.toString()); is received on 

and then used to call the fetchProducsByName with this string.

fetchProducsByName  launch the query with a like clausule using the wildcard “%”  that means that any character could be before or after the string filter typed by the user.

Great!.

Make The Purchase

The user add products to the cart the only action is push the “Add To Cart” button.

MyFirstStore Products List-Added-Product

MyFirstStore-Products-List-Added-Product

 

 

To resolve this we have to included a custom data adapter called ProductCustomCursorAdapter. Why?.

The motive is that the SimpleCursorAdapter included in Android SDK only can map the editable fields of data in the listview item. The button “Add” is an adicional element no editable component, and we have to build an extension, but it’s easy:

 

Basically the behind idea is extends SimpleCursorAdapter and overwrite the bindview method adding an listener for the addCartButton. Inside the listener we are looking for the productCode, that this code is got from the nameProduct textview.

The bindview methods is executed for all items in the list so we have an listener for each add button.

Really Great!

With the Show Cart action we are acceding to the final Purchase List, with a summary and filters. In the Landscape orientation the view is the next:

MyFirstStore-Purchases-List-Landscape-Cart

MyFirstStore-Purchases-List-Landscape-Cart

Admin Interaction

Create Product

Again is an Admin task, from admin menu we are updated the appBar with the “Introduce” option.

Myfirststore menu create product

Myfirststore menu create product

The input from layout is easy and control when there are empty fields and also it the product exists previously.

When all was ok, the product is added to Product table on Shop database.

 

Delete Product

Remove a product is a task of admin, and is executed from the Product List by a click/touch over the item.

A pop-up request to delete a product in this case we are getting the ProductID by access to the listPosition of the selected item, and then getting a cursor from this and locating the product. A cursor is a only a representation of a pair of data store into the list (no necessary has to return always from data base)

Myfirststore delete a product

Myfirststore delete a product

 

If the dialog is accepted the item is removed with the deleteProductID method in ProductDBAdapter

 Great Again!

Audit All Purchases

Similar to the list purchases or product here we have a filter that let you query by user (spinner list or enter string), product (by string).

MyFirstStore Admin List Purchases

MyFirstStore Admin List Purchases

 

Video Final Persistence Project  “My First Store”

Structure & Persistence Classes

User Activity My Store

Admin My Store

 Conclusions

In a summary work with different storage levels:

  • How complete a navigation of a base app with Menu Bar and CRUD actions.
  • How initialize data of my app (demo way).
  • Login validations and register.
  • Structure and manage database adaptador with SQLite.
  • Filter data in activities and query.
  • Delete Product data.
  • Manage and Admin roles.

Remember all files are deleted when uninstalled the app.

Good job, now you are prepared to move to advanced level, you get along very well.

 

Download Full Example CRUD MyFirstStore App

MyFirstStore

All Downloads

https://bitbucket.org/keikisacademy/keikis-academy-android

git clone https://bitbucket.org/keikisacademy/keikis-academy-android.git

Info Links

Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
https://developer.android.com/guide/topics/data/data-storage.html

 

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

One comment

  1. Wonderful chapter, with sources & videos.
    Congratulations

Leave a Reply

Social media & sharing icons powered by UltimatelySocial