2015-04-25 46 views
0

嗨,在下面的代碼中,我爲表格格式顯示錶格佈局。但在我的活動中只顯示標題。但值不顯示。json數據在android中的表格佈局中不顯示

在下面的代碼數據字符串也顯示值

任何一個可以幫助我

的Java

public class General_patient_list extends Activity { 

    String data = ""; 
    TableLayout tl; 
    TableRow tr; 
    TextView label; 
    String queryString; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.general_collection_report); 

     tl = (TableLayout) findViewById(R.id.maintable); 
     final String date1 = getIntent().getStringExtra("date1"); 
     final String date2 = getIntent().getStringExtra("date2"); 


     new Thread(new Runnable() { 
      public void run() { 
       queryString = "date1=" + date1 + "&date2=" 
         + date2; 
       data = DatabaseUtility.executeQueryPhp("general_registration",queryString); 
       System.out.println(data); 

       runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         ArrayList<Patients> patients = parseJSON(data); 
         addData(patients);      
        } 
       }); 

      } 
     }).start(); 
    } 

    public ArrayList<Patients> parseJSON(String result) { 
     ArrayList<Patients> patients = new ArrayList<Patients>(); 
     try { 
      JSONArray jArray = new JSONArray(result); 
      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject json_data = jArray.getJSONObject(i); 
       Patients patient = new Patients(); 
       //patient.setSi(json_data.getInt("si")); 
       patient.setRegno(json_data.getString("reg_no")); 
       // patient.setPat_name(json_data.getString("pat_name")); 
       patient.setSex(json_data.getString("sex")); 
       patient.setReg_date(json_data.getString("reg_date")); 
       patient.setAddress(json_data.getString("address")); 
       patients.add(patient); 
      } 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
     return patients; 
    } 

    void addHeader(){ 
     /** Create a TableRow dynamically **/ 
     tr = new TableRow(this); 

     /** Creating a TextView to add to the row **/ 
     // label = new TextView(this); 
     //label.setText("Si"); 
     // label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
     //   LayoutParams.WRAP_CONTENT)); 
     //label.setPadding(5, 5, 5, 5); 
     //label.setBackgroundColor(Color.RED); 
     // LinearLayout Ll = new LinearLayout(this); 
     // LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
     //   LayoutParams.WRAP_CONTENT); 
     // params.setMargins(5, 5, 5, 5); 
     //Ll.setPadding(10, 5, 5, 5); 
     // Ll.addView(label,params); 
     // tr.addView((View)Ll); // Adding textView to tablerow. 

     /** Creating Qty Button **/ 
     TextView reg_no = new TextView(this); 
     reg_no.setText("Reg_No"); 
     reg_no.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     reg_no.setPadding(5, 5, 5, 5); 
     reg_no.setBackgroundColor(Color.RED); 
     LinearLayout Ll = new LinearLayout(this); 
     Ll = new LinearLayout(this); 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
         LayoutParams.WRAP_CONTENT); 
     params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 5, 5, 5); 
     //Ll.setPadding(10, 5, 5, 5); 
     Ll.addView(reg_no,params); 
     tr.addView((View)Ll); // Adding textview to tablerow. 

     /** Creating Qty Button **/ 
     TextView pat_name = new TextView(this); 
     pat_name.setText("Patient Name"); 
     pat_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     pat_name.setPadding(5, 5, 5, 5); 
     pat_name.setBackgroundColor(Color.RED); 
     Ll = new LinearLayout(this); 
     params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 5, 5, 5); 
     //Ll.setPadding(10, 5, 5, 5); 
     Ll.addView(pat_name,params); 
     tr.addView((View)Ll); // Adding textview to tablerow. 

     /** Creating Qty Button **/ 
     TextView sex = new TextView(this); 
     sex.setText("Sex"); 
     sex.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     sex.setPadding(5, 5, 5, 5); 
     sex.setBackgroundColor(Color.RED); 
     Ll = new LinearLayout(this); 
     params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 5, 5, 5); 
     //Ll.setPadding(10, 5, 5, 5); 
     Ll.addView(sex,params); 
     tr.addView((View)Ll); // Adding textview to tablerow. 

     /** Creating Qty Button **/ 
     TextView reg_date = new TextView(this); 
     reg_date.setText("Registration Date"); 
     reg_date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     reg_date.setPadding(5, 5, 5, 5); 
     reg_date.setBackgroundColor(Color.RED); 
     Ll = new LinearLayout(this); 
     params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 5, 5, 5); 
     //Ll.setPadding(10, 5, 5, 5); 
     Ll.addView(reg_date,params); 
     tr.addView((View)Ll); // Adding textview to tablerow. 

     /** Creating Qty Button **/ 
     TextView address = new TextView(this); 
     address.setText("Address"); 
     address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     address.setPadding(5, 5, 5, 5); 
     address.setBackgroundColor(Color.RED); 
     Ll = new LinearLayout(this); 
     params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT); 
     params.setMargins(0, 5, 5, 5); 
     //Ll.setPadding(10, 5, 5, 5); 
     Ll.addView(address,params); 
     tr.addView((View)Ll); // Adding textview to tablerow. 

     // Add the TableRow to the TableLayout 
     tl.addView(tr, new TableLayout.LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
    } 

    @SuppressWarnings({ "rawtypes", "deprecation" }) 
    public void addData(ArrayList<Patients> patients) { 

     addHeader(); 

     for (Iterator i = patients.iterator(); i.hasNext();) { 

      Patients p = (Patients) i.next(); 

      /** Create a TableRow dynamically **/ 
      tr = new TableRow(this); 

      /** Creating a TextView to add to the row **/ 
      //label = new TextView(this); 
      // label.setText(p.getSi()); 
      // label.setId(p.getSi()); 
      // label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
      //   LayoutParams.WRAP_CONTENT)); 
      // label.setPadding(5, 5, 5, 5); 
      // label.setBackgroundColor(Color.GRAY); 
      // LinearLayout Ll = new LinearLayout(this); 
      // LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
     //   LayoutParams.WRAP_CONTENT); 
     // params.setMargins(5, 2, 2, 2); 
      //Ll.setPadding(10, 5, 5, 5); 
     // Ll.addView(label,params); 
     // tr.addView((View)Ll); // Adding textView to tablerow. 

      /** Creating Qty Button **/ 
      TextView reg_no = new TextView(this); 
      reg_no.setText(p.getRegno()); 
      reg_no.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      reg_no.setPadding(5, 5, 5, 5); 
      reg_no.setBackgroundColor(Color.GRAY); 
      LinearLayout Ll = new LinearLayout(this); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      Ll = new LinearLayout(this); 
      params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 2, 2, 2); 
      //Ll.setPadding(10, 5, 5, 5); 
      Ll.addView(reg_no,params); 
      tr.addView((View)Ll); // Adding textview to tablerow. 

      /** Creating Qty Button **/ 
      TextView pat_name = new TextView(this); 
      pat_name.setText(p.getPat_name()); 
      pat_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      pat_name.setPadding(5, 5, 5, 5); 
      pat_name.setBackgroundColor(Color.GRAY); 
      Ll = new LinearLayout(this); 
      params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 2, 2, 2); 
      //Ll.setPadding(10, 5, 5, 5); 
      Ll.addView(pat_name,params); 
      tr.addView((View)Ll); // Adding textview to tablerow. 

      /** Creating Qty Button **/ 
      TextView sex = new TextView(this); 
      sex.setText(p.getSex()); 
      sex.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      sex.setPadding(5, 5, 5, 5); 
      sex.setBackgroundColor(Color.GRAY); 
      Ll = new LinearLayout(this); 
      params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 2, 2, 2); 
      //Ll.setPadding(10, 5, 5, 5); 
      Ll.addView(sex,params); 
      tr.addView((View)Ll); // Adding textview to tablerow. 

      /** Creating Qty Button **/ 
      TextView reg_date = new TextView(this); 
      reg_date.setText(p.getReg_date()); 
      reg_date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      reg_date.setPadding(5, 5, 5, 5); 
      reg_date.setBackgroundColor(Color.GRAY); 
      Ll = new LinearLayout(this); 
      params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 2, 2, 2); 
      //Ll.setPadding(10, 5, 5, 5); 
      Ll.addView(reg_date,params); 
      tr.addView((View)Ll); // Adding textview to tablerow. 

      /** Creating Qty Button **/ 
      TextView address = new TextView(this); 
      address.setText(p.getAddress()); 
      address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      address.setPadding(5, 5, 5, 5); 
      address.setBackgroundColor(Color.GRAY); 
      Ll = new LinearLayout(this); 
      params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 2, 2, 2); 
      //Ll.setPadding(10, 5, 5, 5); 
      Ll.addView(address,params); 
      tr.addView((View)Ll); // Adding textview to tablerow. 

      // Add the TableRow to the TableLayout 
      tl.addView(tr, new TableLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
     } 
    } 

回答

0

嘗試更換爲循環如下

for (int i=0; i<patients.size(); i++) { 
    Patients p = patients.get(i); 

} 

還要檢查病人是否排列是空的或不是通過調試。