Android Cascading Spinners Using Php Mysql

Android Cascading Spinner – In this post we will see how we can populate second spinner dynamically based on selection of first spinner. We will select a category from first spinner then we will fetch data from mysql database according to the selected category.So let’s start with php, mysql part.

Installing XAMPP Server

Before do anything firstly check you have XAMPP Server on your system. Alternatively you can use WAMP  (For Windows), LAMP (For Linux) and MAMP (For MAC).

If you don’t have XAMPP Server you can Download and Install form https://www.apachefriends.org/download.html

Start Apache and Mysql form XAMPP Control Panel.

Creating MySql Database

In this tutorial to demonstrate we will take two Spinners. First Spinner is for loading Mobile Names and Second for to load Mobile Model Names of that Mobile which we selected from first spinner.

Open phpmyadmin to open this type localhost/phpmyadmin in Internet Browser and Create a Database and Table.

Creating Database

Creating model table

Inserting Data

Creating PHP Files

In this part we will create 3 files.

  • config.php – This file will have Database Name, Username and Password.
  • DbConnect.php – A class to open and close the database connection.
  • get_model.php – To fetch Model Name of Mobiles from Database.

Open htdocs folder under C:\xampp and Create folder CascedingSpinner .In this folder create file-

  1. config.php- In this file we mentioned database name, username, password and host name. If your mysql database is having a password, don’t forget to define it here.
  2. DbConnect.php – A class to open and close the database connection.
  3. get_model.php – Here we simply open the database connection, catch Mobile Name from Query String , run a select query and build a response array and echo the json.

    We are done with server side part. Let’s move to Android Part.

Creating New Android Project – Android Cascading Spineer

  • Create a new project in Android Studio File–>New–>New Project.
  • Enter Application Name, Package Name, Select Empty Activity and click on finish.
  • Add library in app build.gradle file –
  • Our application will use Internet to access data so firstly we need to add INTERNET permission in AndroidManifest.xml file. Open your AndroidManifest.xml file and add following permission.<uses-permission android:name=”android.permission.INTERNET”/>
  • Create a class file named Model.java. This model class will be useful to convert json data into objects.
  • Create a class named Handler.java and write the following code. makeServiceCall() method should be called to make http calls.
  • Open Values–>Strings.xml file and put values for arrary.

  • Now we will design the interface for our main activity. We have two Spinners on our layout first will load Mobile name and  a Second spinner to show the Model Name fetched from MySQL database. Open activity_main.xml file and insert the following code.
  • Open your MainActivity.java file add the basic code like declaring required variables, spinner listener etc. we defined an Async method to fetch list of Model Name from MySQL and showing them in spinner. Add the following code after onCreate block in MainActivity.java. I also added another method populateSpinnerModel() to takes care of loading the data into spinner. This async method should be called in onCreate method like new GetModelFromServer().execute(). complete code of MainActivity.java

Screen Shot

Android Cascading Spinners

Download Source Code

Download Here >>

Subscribe To Our YouTube Channel

 

Like Us On Facebook

 

RECENT POST

5 Comments

    1. Here i am using Get method. I will upload post on how you can pass params using post method as soon as possible .

    1. Please add values of array in values–>strings.xml file

      < %resources>
      < %string name="app_name">CascadingSpinners< %/string>
      < %string-array name="mobile_arrays">
      < %item>Apple< %/item >
      < %item>Samsung< %/item>
      < %item>Xiaomi< %/item>
      < %/string-array>
      < %/resources>

      Remove % before use

  1. And how can I do it if I need two tables not juste one and the value of the first spinner based on the second one?

Leave a Reply

Your email address will not be published. Required fields are marked *