我在android studio中使用mysql。 我想将默认提示设置为微调框。我还尝试过通过Spinner属性从XML设置相同的值以进行提示。但这不能正常工作。下面是我的代码。
public static final String CollegeNamearray = "college";
public static final String JSON_ARRAY = "result";
public static final String JSON_ID = "id";
private JSONArray result;
Spinner spinner;
spinner = (Spinner) findViewById(R.id.spinner);
arrayList = new ArrayList<String>();
getdata();
shared=getSharedPreferences(AppPreference.USER_DETAILS, Context.MODE_PRIVATE);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
item = getcollegeid(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
txtview1.setText("");
}
});
private void getdata() {
final StringRequest stringRequest = new StringRequest(URLs.HttpUrlgetcollege,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(JSON_ARRAY);
collegelist(result);
} catch (JSONException e) {
e.printStackTrace();
}
// Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void collegelist(JSONArray j) {
for (int i = 0; i < j.length(); i++) {
try {
JSONObject json = j.getJSONObject(i);
arrayList.add(json.getString(CollegeNamearray));
} catch (JSONException e) {
e.printStackTrace();
}
}
spinner.setAdapter(new ArrayAdapter<String>(login.this, android.R.layout.simple_spinner_dropdown_item, arrayList));
}
请提供解决方案以在加载数据之前设置提示。
预先感谢。
答案 0 :(得分:0)
您需要像下面一样更改此代码。您需要添加默认值作为微调器的第一个元素
private void collegelist(JSONArray j) {
//add default value as first element of spinner
arrayList.add("default value")
for (int i = 0; i < j.length(); i++) {
......
}