第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Android / MySQL:Spinner不填充MySQL中的數(shù)據(jù)

Android / MySQL:Spinner不填充MySQL中的數(shù)據(jù)

PHP
ibeautiful 2022-08-05 15:36:25
我的代碼有問(wèn)題。我已經(jīng)創(chuàng)建了一個(gè)從MySQL數(shù)據(jù)庫(kù)填充的微調(diào)器。PHP代碼似乎根本沒(méi)有問(wèn)題,因?yàn)槲疫\(yùn)行鏈接“l(fā)ocalhost/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”}]}但是當(dāng)我打開(kāi)應(yīng)用程序時(shí),微調(diào)器不會(huì)顯示數(shù)據(jù)。我不知道為什么。以下是我的代碼爪哇島公共類(lèi) MainActivity 擴(kuò)展 AppCompatActivity 實(shí)現(xiàn) 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();                        }                    }
查看完整描述

1 回答

?
慕姐8265434

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊

我在我的安卓工作室里執(zhí)行了你的代碼。我看起來(lái)您的網(wǎng)絡(luò)調(diào)用無(wú)法獲取數(shù)據(jù)(結(jié)果保持為空)。檢查我的代碼以供參考。我用靜態(tài)數(shù)據(jù)替換了您的網(wǎng)絡(luò)呼叫。


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

}

}



查看完整回答
反對(duì) 回復(fù) 2022-08-05
  • 1 回答
  • 0 關(guān)注
  • 88 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)