json数据不在android中的表格布局中显示

时间:2015-04-25 09:54:28

标签: android json

您好,在下面的代码中,我正在显示表格式的表格布局。但在我的活动中只显示了标题。但是没有显示值。

在下面的代码数据String中还显示值

任何人都可以帮助我

的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));
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试替换循环,如下所示

Specific Version

还通过调试检查患者阵列是否为空。