我的代码目前正在工作并显示数据库中的一个项目,但在线查看后,我将数据放入适配器中,但它只显示数据库中的第一项。我应该更改什么才能显示数据库的所有项目?
MarksActivity.java
val
答案 0 :(得分:0)
最后一个值必须显示。我建议你初始化上面循环的String [] Status并在循环内添加状态。你会得到结果!
创建一个模型类作为DemoObject:
public class DemoObject {
private String nota;
public String getNota() {
return nota;
}
public void setNota(String nota) {
this.nota = nota;
}
}
现在,
private void showJSON(String response){
DemoObject demoObject = new DemoObject();
List<DemoObject> list = new ArrayList();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
for(i=0;i<result.length();i++)
Log.v("Result--",""+result.getString(i));
JSONObject collegeData = result.getJSONObject(0);
demoObject.setNota(collegeData.getString(Config.KEY_NAME));
list.add(demoObject);
} catch (JSONException e) {
e.printStackTrace();
}
最后,
textViewResult.setAdapter(new ArrayAdapter<String>(MarksActivity.this,
android.R.layout.simple_list_item_1, list));
答案 1 :(得分:0)
问题出在showJson
方法中
你需要在开头初始化字符串数组并在那里添加项目
private void showJSON(String response){
List<String> status = new ArrayList<String>(); // since you don't know exactly how many elements are there in the json array
String nota="";
String materie="";
String profesor = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
for(i=0;i<result.length();i++) {
Log.v("Result--",""+result.getString(i));
JSONObject collegeData = result.getJSONObject(i);
nota = collegeData.getString(Config.KEY_NAME);
materie = collegeData.getString(Config.KEY_ADDRESS);
profesor = collegeData.getString(Config.KEY_VC);
status.add("Nota:\t"+nota+"\nMaterie:\t" +materie+ "\nProfesor:\t"+ profesor);
}
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setAdapter(new ArrayAdapter<String>(MarksActivity.this,
android.R.layout.simple_list_item_1, status));
}
答案 2 :(得分:0)
根据我的理解,你的showJSON方法应如下所示:
private void showJSON(String response){
String nota="";
String materie="";
String profesor = "";
String[] status = null;
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
status = new String[result.length()];
for(i=0;i<result.length();i++) {
Log.v("Result--",""+result.getString(i));
JSONObject collegeData = result.getJSONObject(i);
nota = collegeData.getString(Config.KEY_NAME);
materie = collegeData.getString(Config.KEY_ADDRESS);
profesor = collegeData.getString(Config.KEY_VC);
status[i] = "Nota:\t"+nota+"\nMaterie:\t" +materie+ "\nProfesor:\t"+ profesor;
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MarksActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, status);
textViewResult.setAdapter(adapter);
}
答案 3 :(得分:0)
private void getData() {
String nume = editTextId.getText().toString().trim();
if (nume.equals("")) {
Toast.makeText(this, "Numele tau", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Te rugam asteapta","Fetching...",false,false);
String url = Config.DATA_URL+editTextId.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MarksActivity.this, error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
List<String> status = new ArrayList<String>(); // since you don't know exactly how many elements are there in the json array
String nota="";
String materie="";
String profesor = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
for(i=0;i<result.length();i++) {
Log.v("Result--",""+result.getString(i));
JSONObject collegeData = result.getJSONObject(i);
nota = collegeData.getString(Config.KEY_NAME);
materie = collegeData.getString(Config.KEY_ADDRESS);
profesor = collegeData.getString(Config.KEY_VC);
status.add("Nota:\t"+nota+"\nMaterie:\t" +materie+ "\nProfesor:\t"+ profesor);
}
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setAdapter(new ArrayAdapter<String>(MarksActivity.this,
android.R.layout.simple_list_item_1, status));
}