2017-04-05 85 views
0

我的代碼目前正在工作並顯示數據庫中的一個項目,但在聯機查看後,我將數據放入適配器中,但它只顯示數據庫中的第一項。我應該更改哪些內容才能顯示數據庫的所有項目?在列表視圖中顯示數據庫項目

MarksActivity.java

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){ 

      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(0); 
       nota = collegeData.getString(Config.KEY_NAME); 
       materie = collegeData.getString(Config.KEY_ADDRESS); 
       profesor = collegeData.getString(Config.KEY_VC); 

      } catch (JSONException e) { 
       e.printStackTrace(); 

      } 



      String[] status = {"Nota:\t"+nota+"\nMaterie:\t" +materie+ "\nProfesor:\t"+ profesor}; 

      textViewResult.setAdapter(new ArrayAdapter<String>(MarksActivity.this, 
        android.R.layout.simple_list_item_1, status)); 
     } 

回答

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)); 
+0

我究竟如何,可以將每個循環內? –

+0

如果我在循環上面初始化String []狀態,它將不起作用 –

+0

編輯帖子! –

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)); 
    } 
+0

我是否需要在我的xml中聲明列表和ArrayList? –

+0

沒有它們在'java.util'包中提供,所以只需添加'import java.util.List;進口的java.util.ArrayList;'在文件的開頭 –

+0

仍然只顯示來自DB –

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); 
} 
+0

無法解析符號狀態 –

+0

仍然只顯示第一項 –

+0

此編輯答案應該起作用。我認爲textViewResult是listview ..和其他定義ArrayAdapter 適配器如上所述,並每次聲明。調試並檢查您是否在結果JSONArray中獲得更多條目。希望這會幫助你.. –

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)); 
} 
相關問題