【发布时间】:2020-01-17 03:20:39
【问题描述】:
我的代码有问题。我已经创建了一个从 MySQL 数据库填充的微调器。 PHP代码似乎完全没有问题,因为我运行了“localhost/latihan1/menu/php”链接,会显示json字符串。
json显示如下:
{"result":[{"username":"haha","name":"Bus Bisnis","course":"math","session":"20119"},{"username":" hihi","name":"Bus Ace","course":"fizik","session":"12817"},{"username":"m_ridwan","name":"Ridwan","course": "Komputer","session":"1920"},{"username":"m_iqbal","name":"Iqbal","course":"Sains","session":"2021"}]}
但是当我打开应用程序时,微调器不显示数据。我不知道为什么。 下面是我的代码
JAVA
公共类 MainActivity 扩展 AppCompatActivity 实现 Spinner.OnItemSelectedListener{
private Spinner spinner;
private ArrayList<String> students;
//JSON Array
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);
//Initializing the ArrayList
students = new ArrayList<String>();
spinner = findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
textViewName = findViewById(R.id.textViewName);
textViewCourse = findViewById(R.id.textViewCourse);
textViewSession = findViewById(R.id.textViewSession);
getData();
}
private void getData(){
StringRequest stringRequest = new StringRequest(Config.DATA_URL,
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));
}
private String getName(int position){
String name="";
try {
JSONObject json = result.getJSONObject(position);
name = json.getString(Config.TAG_NAME);
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
private String getCourse(int position){
String course="";
try {
JSONObject json = result.getJSONObject(position);
course = json.getString(Config.TAG_COURSE);
} catch (JSONException e) {
e.printStackTrace();
}
return course;
}
private String getSession(int position){
String session="";
try {
JSONObject json = result.getJSONObject(position);
session = json.getString(Config.TAG_SESSION);
} catch (JSONException e) {
e.printStackTrace();
}
return session;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
textViewName.setText(getName(position));
textViewCourse.setText(getCourse(position));
textViewSession.setText(getSession(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
textViewName.setText("");
textViewCourse.setText("");
textViewSession.setText("");
}
}
menu.php
<?php
require_once "koneksi.php";
$query = mysqli_query($con, "SELECT * FROM kendaraan ORDER BY id ASC");
$students = array();
while($row = mysqli_fetch_array($query)){
array_push($students,array(
'username'=>$row['username'],
'name'=>$row['name'],
'course'=>$row['course'],
'session'=>$row['session']
));
}
echo json_encode(array('result'=>$students));
mysqli_close($con);
?>
【问题讨论】:
标签: java php android mysql spinner