Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch data from MySQL database and Load it to Spinner?

Tags:

android

I have my school project where I need to fetch values from MySQL database and display it to Spinner in Android. I am using Android Studio and Volley Library for the networking operation but confused how to achieve this thing. I'll appreciate your help. Thank You :)

like image 553
Emma Wilson Avatar asked Oct 31 '15 15:10

Emma Wilson


People also ask

How to use MySQL with spinner in Android?

Android MySQL Tutorial with Spinner. How to Insert, Select then Show data From MySQL into Spinner widget. Lets see how to perform basic data entry and retrieval into MySQL database from our android app. First we insert multi-column data to MySQL from android.

How to fetch data from MySQL database in PHP and display?

Use the below given two steps and fetch data from MySQL database in PHP and display in HTML table: 1. Connecting to the database in PHP In this step, you will create a file name db.php and update the below code into your file.

How to load data from file to table in MySQL?

To load data from file to table in MySQL, you can either opt for writing SQL statements or use the GUI (Graphical User Interface) of MySQL Workbench, phpMyAdmin, or other data integration tools. In this article, you will learn how to effectively load data from file to table in MySQL using 3 different methods.

How to create a MySQL database connection in PHP?

1. Connecting to the database in PHP In this step, you will create a file name db.php and update the below code into your file. The below code is used to create a MySQL database connection in PHP. When we fetch, insert, update or delete data from MySQL database, there we will include this file: 2. Fetch data from the database and display in table


1 Answers

You have to first create a php script that will print the data of your mysql database in json format.. Here I am showing you an example lets see this is my database MySQL Database

Now suppose I need to load all the username of this table to android's spinner. So this is the php script that will give me the data in json format

<?php 

$sql = "SELECT * FROM students";

require_once('dbConnect.php');

$r = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($r)){
    array_push($result,array(
        'username'=>$row['username'],
        'name'=>$row['name'],
        'course'=>$row['course'],
        'session'=>$row['session']
    ));
}

echo json_encode(array('result'=>$result));

mysqli_close($con);

The above php code will give the following json

{"result":[{"username":"faiz","name":"Faiz Khan","course":"BCA","session":"2014-2017"},{"username":"belal","name":"Belal Khan","course":"MCA","session":"2015-2018"},{"username":"ramiz","name":"Ramiz Khan","course":"MBA","session":"2015-2017"},{"username":"zafar","name":"Zafar Khan","course":"MBA","session":"2014-2016"}]}

Now android code will be MainActivity.java

public class MainActivity extends AppCompatActivity{
private Spinner spinner;
private ArrayList<String> students;
private JSONArray result;
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    students = new ArrayList<String>();
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(this);
    textViewName = (TextView) findViewById(R.id.textViewName);
    textViewCourse = (TextView) findViewById(R.id.textViewCourse);
    textViewSession = (TextView) findViewById(R.id.textViewSession);
    getData();
}

private void getData(){
    StringRequest stringRequest = new StringRequest("your php script address",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        j = new JSONObject(response);
                        result = j.getJSONArray(Config.JSON_ARRAY);
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void getStudents(JSONArray j){
    for(int i=0;i<j.length();i++){
        try {
            JSONObject json = j.getJSONObject(i);
            students.add(json.getString(Config.TAG_USERNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students));
}
}

Source Android Spinner Example with MySQL Database

like image 100
Belal Khan Avatar answered Sep 28 '22 06:09

Belal Khan