我已经采用微调器并且在mysql中有数据库我现在想在android中的spinner上从数据库中检索数据或名字,我可以在微调器上显示数据,但是当我选择其中一个时,微调器变为空白(数据未选中)
但是当我添加
ar.add("");
所选项目可以在微调器中显示。
任何人都可以帮助修复我的代码。
非常感谢--- terima kasih:)
这是我的代码:
package com.app.example.mysqltest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
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.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
TextView result;
RequestQueue requestQueue;
String showUrl = "http://10.10.10.1/trial/mysql/showStudent.php";
ArrayList<String> ar = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.result);
requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray students = response.getJSONArray("students");
for (int i = 0; i < students.length(); i++ ){
JSONObject student = students.getJSONObject(i);
String firstname = student.getString("firstname");
ar.add(firstname);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, ar);
adapter.notifyDataSetChanged();
} catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
**//ar.add("");**
Spinner dropdown = (Spinner)findViewById(R.id.planets_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, ar);
dropdown.setAdapter(adapter);
}
}
答案 0 :(得分:0)
这是一个非常着名的问题,您尝试异步获取数据,并且在收到数据后,您没有通过notifyDataSetChanged
更新到适配器。
在for
循环后尝试此操作:adapter.notifyDataSetChanged()
像这样:
Spinner dropdown = (Spinner)findViewById(R.id.planets_spinner);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, ar);
dropdown.setAdapter(adapter);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray students = response.getJSONArray("students");
for (int i = 0; i < students.length(); i++ ){
JSONObject student = students.getJSONObject(i);
String firstname = student.getString("firstname");
ar.add(firstname);
}
// Just 1 LOC
adapter.notifyDataSetChanged();
} catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);