Android / MySQL:Spinner不填充MySQL中的数据

我的代码有问题。我已经创建了一个从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”}]}


但是当我打开应用程序时,微调器不会显示数据。我不知道为什么。以下是我的代码


爪哇岛


公共类 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();

                        }

                    }

ibeautiful
浏览 77回答 1
1回答

慕姐8265434

我在我的安卓工作室里执行了你的代码。我看起来您的网络调用无法获取数据(结果保持为空)。检查我的代码以供参考。我用静态数据替换了您的网络呼叫。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 Arrayprivate JSONArray result;private TextView textViewName;private TextView textViewCourse;private TextView textViewSession;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main5);&nbsp; &nbsp; //Initializing the ArrayList&nbsp; &nbsp; students = new ArrayList<String>();&nbsp; &nbsp; spinner = findViewById(R.id.spinner);&nbsp; &nbsp; spinner.setOnItemSelectedListener(this);&nbsp; &nbsp; textViewName = findViewById(R.id.textViewName);&nbsp; &nbsp; textViewCourse = findViewById(R.id.textViewCourse);&nbsp; &nbsp; textViewSession = findViewById(R.id.textViewSession);&nbsp; &nbsp; getData();}private void getData(){&nbsp; &nbsp; 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\"}]}";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(json);&nbsp; &nbsp; &nbsp; &nbsp; result= jsonObject.getJSONArray("result");&nbsp; &nbsp; &nbsp; &nbsp; getStudents(result);&nbsp; &nbsp; } catch (Exception e){&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}private void getStudents(JSONArray j) {&nbsp; &nbsp; for (int i = 0; i < j.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = j.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students.add(json.getString(Config.TAG_USERNAME));&nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, students));}private String getName(int position) {&nbsp; &nbsp; String name = "";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = result.getJSONObject(position);&nbsp; &nbsp; &nbsp; &nbsp; name = json.getString(Config.TAG_NAME);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return name;}private String getCourse(int position) {&nbsp; &nbsp; String course = "";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = result.getJSONObject(position);&nbsp; &nbsp; &nbsp; &nbsp; course = json.getString(Config.TAG_COURSE);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return course;}private String getSession(int position) {&nbsp; &nbsp; String session = "";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject json = result.getJSONObject(position);&nbsp; &nbsp; &nbsp; &nbsp; session = json.getString(Config.TAG_SESSION);&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return session;}@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; //Setting the values to textviews for a selected item&nbsp; &nbsp; textViewName.setText(getName(position));&nbsp; &nbsp; textViewCourse.setText(getCourse(position));&nbsp; &nbsp; textViewSession.setText(getSession(position));}@Overridepublic void onNothingSelected(AdapterView<?> parent) {&nbsp; &nbsp; textViewName.setText("");&nbsp; &nbsp; textViewCourse.setText("");&nbsp; &nbsp; textViewSession.setText("");}}
打开App,查看更多内容
随时随地看视频慕课网APP