嗨我会询问我的脚本,当我在插入的mysql数据库数据中插入新数据但没有立即添加到微调器时,所以我无法选择刚刚输入的数据。 (我们必须旋转手机才能显示我刚输入的数据)
插入数据后,我称之为:
requestQueue.add(jsonObjectRequest);
,但它失败了:(,旋转器仍然没有刷新数据。 我的代码有什么问题?
非常感谢--- grazie:)
这是我的代码:
package com.app.example.nyoba;
import....
public class MainActivity extends AppCompatActivity {
EditText firstname, lastname, age;
Button insert, show;
TextView result;
RequestQueue requestQueue;
String insertUrl = "http://10.10.10.1/trial/mysql/insertStudent.php";
String showUrl = "http://10.10.10.1/trial/mysql/showStudent.php";
ArrayList<String> ar = new ArrayList<String>();
JsonObjectRequest jsonObjectRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname = (EditText) findViewById(R.id.editText);
lastname = (EditText) findViewById(R.id.editText2);
age = (EditText) findViewById(R.id.editText3);
insert = (Button) findViewById(R.id.insert);
show = (Button) findViewById(R.id.show);
result = (TextView) findViewById(R.id.result);
requestQueue = Volley.newRequestQueue(getApplicationContext());
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray students = response.getJSONArray("students");
//set TextView blank and scrollable
result.setText("");
result.setMovementMethod(new ScrollingMovementMethod());
for (int i = 0; i < students.length(); i++) {
JSONObject student = students.getJSONObject(i);
String firstname = student.getString("firstname");
String lastname = student.getString("lastname");
String age = student.getString("age");
result.append(firstname + " " + lastname + " " + age + "\n");
}
result.append("===\n");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
});
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
Log.w("RESPONSE", "" + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("firstname", firstname.getText().toString());
parameters.put("lastname", lastname.getText().toString());
parameters.put("age", age.getText().toString());
return parameters;
}
};
requestQueue.add(request);
requestQueue.add(jsonObjectRequest);
}
});
jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Spinner dropdown = (Spinner)findViewById(R.id.planets_spinner);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item, ar);
dropdown.setAdapter(adapter);
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);
}
adapter.notifyDataSetChanged();
Log.w("CHECK", "DI CLASS= " + adapter.getCount());
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
// get selected item
Spinner dropdown = (Spinner)findViewById(R.id.planets_spinner);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Selected= " + parent.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}