Send user entered recyclerview edittext Array list data to database (phpmyadmin)
let’s begin,
Database Setup
My database is hosted with my website hosting protocoderspoint.com, so in my hosting i have created a new database special for this tutorial.
Create a new table in your phpmyadmin database
How a look at database, it has a table by name :
‘ExpenseTable’ which has 3 column
- id : type int auto Increment.
- ExpenseName : type varchar(50).
- ExpenseAmt : type varchar(50).
command to create table in your database
Php code – connect database – storing array list to data table
Here are 2 php files/code
- connect.php: Help us in getting connected with our database.
- save_expense.php: Helps us in storing/inserting data into our database.
connect.php
The connect.php is a common php code that help in getting connected with our database, I have created connect.php file separate so that in my other php code i can simply include connect.php code that’s it, whenever i hit or call other php code where connet.php is included it automatically get connected to out database.
save_expense.php
Then, in above save_expense.php code i m sending list of jsonArray string from our android app using POST method.
Snippet code Explaination
$expname = $_POST[“expname”];
$expname = json_decode($expname,TRUE);
Here the PHP code is receiving ArrayList in the form of JSON ARRAY, that is been decoded using the json_decode function.
After that we are using for loop to iterate the total number of data present in the array list using count($expname) this will return a number, for example, suppose your array is [‘0’, ‘1’, ‘2’, ‘3’] then count will return 4, so the for loop will run for 4 times.
Then in for loop, we have a SQL query then will insert the data into our database using mysqli_query.
Ok so now we are done with our php script that will insert data into our database, now let’s go to android coding.
Recyclerview EditText Entered data store in an array and send to database
android project structure
Have a look at my android project structure for easy understanding of the android tutorial on Recyclerview with editText Field.
Check out this tutorial article “Android RecyclerView with EditText Example + Expense sum calculation” to know the UI and the calculation part and how i store user entered data in arraylist.
Android Volley library is been used to send data from android to php build.gradle (module: app) under dependencies section add the volley library.
itemdatamodel.java
Our DataModel class holder 2 kinds of datatype i.e integer and a string.
integer to hold Expense Id and String that holds ExpenseName.
MainActivity.java
Here i have created static data, and sending it to our Adapter to display the data in our android recyclerview.
activity_main.xml
Adapter.java
I recommend to watch the video tutorial to understand what is going on in below android Adapter.java class.
card_prod_list.xml
Network Security Config
So as we are making internet call to send data to our database, we need to specify which domain or IP we are using to send/receive data, so for that, you need to create a networksecurityconfig.xml file in res>xml>
create a directory “XML” inside res then create an XML resource file networksecurityconfig.xml under that directory and copy-paste the below code.
Then go to your AndroidManifest.xml and add the network SecurityConfig file under <application> tag
Post a Comment
0Comments