亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android / MySQL:微調器不填充來自 MySQL 的數據

Android / MySQL:微調器不填充來自 MySQL 的數據

PHP
哆啦的時光機 2022-07-22 16:59:31
我的代碼有問題。我已經創建了一個從 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{<?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);?>
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

我在我的 android studio 中執行了你的代碼。我看起來您的網絡調用無法獲取數據(結果仍然為空)。檢查我的代碼以供參考。我用靜態數據替換了您的網絡調用。


package com.attiq.testapp;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;


import com.google.gson.Gson;

import com.google.gson.JsonArray;

import com.google.gson.JsonObject;


import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;


import java.util.ArrayList;


public class Main5Activity extends AppCompatActivity implements 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_main5);


    //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(){

    String 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\"}]}";

    try {

        JSONObject jsonObject = new JSONObject(json);

        result= jsonObject.getJSONArray("result");

        getStudents(result);

    } catch (Exception e){

        e.printStackTrace();

    }

}


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>(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("");

}

}



查看完整回答
反對 回復 2022-07-22
  • 1 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號