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 :)
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.
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.
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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With