我需要解决此问题的方法。目前,我已观看此视频https://www.youtube.com/watch?v=EBGdWPBdfPo&t=104s。我需要使用自己的数据进行绘图。我已经将数据从mysql保存到php,但是我不知道如何在android studio中显示数据。请帮我。 这是我的php代码:
<?php
require_once 'connect.php';
//checking if any error occured while connecting
if(mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
//creating a query
$stmt = $conn->prepare("SELECT AVG(rb_fd),AVG(rb_check_in),AVG(rb_check_out),AVG(rb_room),AVG(rb_staff),AVG(rb_navigation),AVG(rb_fs),AVG(rb_decor),AVG(rb_facilities),AVG(rb_wifi) FROM rating;");
//executing the query
$stmt->execute();
//binding result to the query
$stmt->bind_result($rb_fd,$rb_check_in,$rb_check_out,$rb_room,$rb_staff,$rb_navigation,$rb_fs,$rb_decor,$rb_facilities,$rb_wifi);
$rating = array();
//traversing throung all the result
while($stmt->fetch()){
$temp = array();
$temp['FD'] = $rb_fd;
$temp['CI'] = $rb_check_in;
$temp['CO'] = $rb_check_out;
$temp['R'] = $rb_room;
$temp['S'] = $rb_staff;
$temp['N'] = $rb_navigation;
$temp['FS'] = $rb_fs;
$temp['D'] = $rb_decor;
$temp['F'] = $rb_facilities;
$temp['W'] = $rb_wifi;
array_push($rating, $temp);
}
//displaying the result in json format
echo json_encode($rating);
?>
这是我检查邮递员时的输出:
[
{
"FD": 2.5,
"CI": 2.5,
"CO": 2.6666666666666665,
"R": 2.5,
"S": 3,
"N": 3.3333333333333335,
"FS": 3,
"D": 2,
"F": 3.5,
"W": 3.6666666666666665
}
]
这是我在android studio中的代码。
package com.example.user.hotelsuriamalaqaapp;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.Rating;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class AdminViewRating extends AppCompatActivity {
private BarChart barChart;
private BarData barData;
private ListView rate;
List<Rate2> ratingList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_view_rating);
//This is code for bar chart
barChart = (BarChart) findViewById(R.id.barchart);
barData = new BarData(getXvalues(), getBarValues());
barData.setValueTextColor(Color.WHITE);
barChart.setData(barData);
barChart.setBackgroundColor(Color.WHITE);
barChart.getXAxis().setTextColor(Color.BLACK);
barChart.setDescription("Rating");
barChart.setGridBackgroundColor(Color.DKGRAY);
barChart.animateXY(3000, 3000);
barChart.invalidate();
rate=findViewById(R.id.rate3);
ratingList=new ArrayList<>();
}
private ArrayList<BarDataSet> getBarValues() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, constant.URL_KIRA,
new Response.Listener<String>() {
ArrayList<BarDataSet> barDataSets;
ArrayList<BarEntry> barEntries = new ArrayList<>();
BarDataSet barDataSet = new BarDataSet(barEntries, "Rates");
barDataSets = new ArrayList<>();
try {
//converting the string to json array object
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray=jsonObject.getJSONArray("rating");
//traversing through all the object
for (int i = 0; i < jsonArray.length(); i++) {
//getting product object from json array
JSONObject object = jsonArray.getJSONObject(i);
ratingList.add(new Rate2(
object.getString("FD"),
object.getString("CI"),
object.getString("CO"),
object.getString("R"),
object.getString("S"),
object.getString("N"),
object.getString("FS"),
object.getString("D"),
object.getString("F"),
object.getString("W")
));
//barEntries.add(new BarEntry(rate,i);
}
barDataSet.setColor(Color.RED);
barDataSets.add(barDataSet);
} catch (JSONException e) {
e.printStackTrace();
}
return barDataSets;
}
//This is coding for x axis
private ArrayList<String> getXvalues(){
ArrayList<String> xvalues = new ArrayList<>();
xvalues.add("FD");
xvalues.add("CI");
xvalues.add("CO");
xvalues.add("R");
xvalues.add("S");
xvalues.add("N");
xvalues.add("FS");
xvalues.add("D");
xvalues.add("F");
xvalues.add("W");
return xvalues;
}
}