我只是想在我的android應用程序中製作一個簡單的微調框。 當我嘗試在微調器中設置項目,但列表中沒有顯示任何項目。從MySQL數據庫填充Android微調 - 列表中沒有項目顯示
我的PHP文件:
[{"id_matkul":"1","kode_matkul":"fffffafafafa","nama_matkul":"adas333","dosen1_matkul":"dddada333","dosen2_matkul":"asdasda"},{"id_matkul":"3","kode_matkul":"asasaaaaa44","nama_matkul":"asas","dosen1_matkul":"asas444","dosen2_matkul":"asa33"}]
我的活動代碼:
package com.qalbistudio.kartukontrolapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class DaftarNilaiUtama1 extends AppCompatActivity {
Spinner sp;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daftar_nilai_utama1);
sp = (Spinner) findViewById(R.id.spinner);
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
JSONArray jsonMainNode = jsonResponse.optJSONArray("response");
List<String> item = new ArrayList<String>();
for(int i = 0; i < jsonMainNode.length(); i++){
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String nama1 = jsonChildNode.optString("id_matkul");
item.add(nama1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DaftarNilaiUtama1.this,android.R.layout.simple_spinner_dropdown_item, item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
SpRequest spreq = new SpRequest(responseListener);
RequestQueue queue = Volley.newRequestQueue(DaftarNilaiUtama1.this);
queue.add(spreq);
}
}
我的服務器請求代碼:
<?php
$con = mysqli_connect("xxxx","xxxx","xxxx","xxxx");
$sql = "select * from mata_kuliah";
$result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));
//create an array
$response = array();
while($row =mysqli_fetch_assoc($result))
{
$response[] = $row;
}
echo json_encode($response);
?>
它以這種形式返回一個JSON字符串
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.Map;
/**
* Created by callbee on 20/05/2016.
*/
public class SpRequest extends StringRequest {
private static final String URL = "http://192.168.1.10/KartuKontrolApp/testing.php";
private Map<String, String> params;
public SpRequest(Response.Listener<String> listener) {
super(Method.POST, URL, listener, null);
}
public Map<String, String> getParams() {
return params;
}
}
我對php不太熟悉,但看起來響應只會是一行 –
@ cricket_007:不,它不僅顯示一行。它顯示我需要的所有我的PHP文件。我編輯了我的文章並添加了我的PHP文件的結果。 – ainunqalbi06
如果您的應用程序正在有效接收有效的JSON對象,您是否驗證過?你想看到針對同一問題的不同方法嗎? –