我使用Android Studio创建了一个活动,该活动由一个表单组成,该表单允许用户向用户希望的表中的数据库添加项目。就设计而言,到目前为止一切都很好。我正在使用微调器,以便从表中检索特定列并在微调器中显示数据,但是,每当我在Emulator / smartphone上运行应用程序时,它都不会显示从数据库中检索到的数据(与数据库的连接)确实成功,并且数据以json
格式在网络浏览器中输出)。有什么建议吗?非常感谢。
在另一台笔记本电脑上,创建了一个新的android studio项目,并将我在此处显示的代码用于测试目的。显然,当我尝试此操作时,微调器起作用了,我不知道为什么,但是我怀疑使用了不同的Gradle或Android Studio版本?
public class add_activity extends AppCompatActivity {
private int PICK_IMAGE_REQUEST = 1;
private static final String typearray = "type_name";
private static final String type = "type_name";
private static final String JSON_ARRAY ="result";
private JSONArray result;
private Button btn_choose ;
Spinner spinner;
private ArrayList<String> arrayList;
TextView food_type;
String type_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
btn_choose = findViewById(R.id.btn_choosedesign);
this.spinner = (Spinner) findViewById(R.id.spinnertype);
food_type = (TextView) findViewById(R.id.hiddentype);
// findViewById(R.id.hiddentype).setVisibility(View.GONE);
arrayList = new ArrayList<String>();
getdata();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
food_type.setText(getlocation(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
food_type.setText("");
}
});
btn_choose.setOnClickListener(new View.OnClickListener() {
...
}
});
}
private void showFileChooser() {
...
}
private void getdata() {
StringRequest stringRequest = new StringRequest("http://.../.../gettype.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
j = new JSONObject(response);
result = j.getJSONArray(JSON_ARRAY);
details(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void details(JSONArray j) {
for (int i = 0; i < j.length(); i++) {
try {
JSONObject json = j.getJSONObject(i);
arrayList.add(json.getString(typearray));
} catch (JSONException e) {
e.printStackTrace();
}
}
this.spinner.setAdapter(new ArrayAdapter<>(add_activity.this, android.R.layout.simple_spinner_dropdown_item, arrayList));
}
//Method to get location name which user selects
private String getlocation(int position) { // method is used to post name of marker to database accordingly
String loc = "";
try {
//Getting object of given index
JSONObject json = result.getJSONObject(position);
//Fetching location from that object
loc = json.getString(type);
} catch (JSONException e) {
e.printStackTrace();
}
//Return location
return loc;
}
}