android表布局中的数据库中没有显示数据


data not displaying from database in android table layout

Hi在我的应用程序中,我设计了一个表单,如果我点击提交按钮,我想在android中以表格格式显示数据。

现在,如果我点击提交按钮,它只显示字段名数据,而没有显示。我是通过使用logcat it打印数据来打印输出的。但logcat给出了一个错误。

参加课堂

public class Attend extends Activity {
    String data = "";
    TableLayout tl;
    TableRow tr;
    TextView label;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tl = (TableLayout) findViewById(R.id.maintable);
        final DataseData getdb = new DataseData();
        new Thread(new Runnable() {
            public void run() {
                data = getdb.getDataFromDB();
                System.out.println(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<Users> users = parseJSON(data);
                        addData(users);                     
                    }
                });
            }
        }).start();
    }
    public ArrayList<Users> parseJSON(String result) {
        ArrayList<Users> users = new ArrayList<Users>();
        try {           
JSONObject jsnobject = new JSONObject(result);
             JSONArray jsonArray = jsnobject.getJSONArray(result);
            for (int i = 0; i < jsnobject.length(); i++) {
                JSONObject explrObject = jsonArray.getJSONObject(i);
                Users user = new Users();
                user.setClass_id(explrObject.getInt("class_id"));
                user.setClass_name(explrObject.getString("class_name"));
                user.setSection_name(explrObject.getString("section_name"));
                users.add(user);
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());  
        }
        return users;
    }
    void addHeader(){
        tr = new TableRow(this);
        label = new TextView(this);
        label.setText("class_id");
        label.setTextColor(Color.BLUE);
        label.setTextSize(15);
        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.addView(label,params);
        tr.addView((View)Ll); 

        TextView class_name = new TextView(this);
        class_name.setText("class_name");
        class_name.setTextSize(15);
        class_name.setTextColor(Color.BLUE);
        class_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        class_name.setPadding(5, 5, 5, 5);
        class_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.addView(class_name,params);
        tr.addView((View)Ll); 

        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tl.removeAllViewsInLayout();
        TextView section_name = new TextView(this);
        section_name.setText("section_name");
        section_name.setTextSize(15);
        section_name.setTextColor(Color.BLUE);
        section_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        section_name.setPadding(5, 5, 5, 5);
section_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.addView(section_name,params);
        tr.addView((View)Ll); 

        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }
    @SuppressWarnings({ "rawtypes" })
    public void addData(ArrayList<Users> users) {
        addHeader();
        for (Iterator i = users.iterator(); i.hasNext();) {
            Users p = (Users) 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.getClass_id());
            label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            label.setPadding(5, 5, 5, 5);
            LinearLayout Ll = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 2, 2, 2);
            Ll.addView(label,params);
            tr.addView((View)Ll);

            TextView class_name = new TextView(this);
            class_name.setText(p.getClass_name());
            class_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            class_name.setPadding(5, 5, 5, 5);
            Ll = new LinearLayout(this);
            params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 3, 3, 3);
            Ll.addView(class_name,params);
            tr.addView((View)Ll); 

            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            TextView section_name = new TextView(this);
            section_name.setText("section_name");
            section_name.setTextSize(15);
            section_name.setTextColor(Color.BLUE);
            section_name.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            section_name.setPadding(10, 0, 0, 0);
    section_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.addView(section_name,params);
            tr.addView((View)Ll); 

            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
    }

submit.php

<?php
$username ='root';
$password ='';
$hostname ='localhost';
$database ='login';
$localhost = mysql_connect($hostname,$username,$password) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database,$localhost);
$i=mysql_query("select class_id,class_name,section_name from class");
$num_rows = mysql_num_rows($i);
while($row = mysql_fetch_array($i))
{
$r[]=$row;
$check=$row['id'];            }
if($check==NULL)
{
$r[$num_rows]="Record is not available";
print(json_encode($r));
}
else
{
$r[$num_rows]="success";
print(json_encode($r));
}
mysql_close($localhost);
?>

Logcat

Error parsing data org.json.JSONException: Value [{"2":"Batch A","1":"Play Group","class_id":"101","0":"101","class_name":"Play Group","section_name":"Batch A"},{"2":"Batch B","1":"Nursery","class_id":"102","0":"102","class_name":"Nursery","section_name":"Batch B"},{"2":"","1":"LKG","class_id":"103","0":"103","class_name":"LKG","section_name":""},{"2":"","1":"Ukg","class_id":"104","0":"104","class_name":"Ukg","section_name":""},{"2":"","1":"I st","class_id":"105","0":"105","class_name":"I st","section_name":""},{"2":"","1":"II","class_id":"106","0":"106","class_name":"II","section_name":""},{"2":"","1":"III","class_id":"107","0":"107","class_name":"III","section_name":""},{"2":"","1":"IV","class_id":"108","0":"108","class_name":"IV","section_name":""},{"2":"","1":"V","class_id":"109","0":"109","class_name":"V","section_name":""},{"2":"","1":"VI","class_id":"110","0":"110","class_name":"VI","section_name":""},{"2":"","1":"VII","class_id":"121","0":"121","class_name":"VII","section_name":""},{"2":"","1":"VIII","class_id":"122","0":"122","class_name":"VIII","section_name":""},{"2":"","1":"IX","class_id":"123","0":"123","class_name":"IX","section_name":""},{"2":"","1":"X","class_id":"124","0":"124","class_name":"X","section_name":""},"success"] of type org.json.JSONArray cannot be converted to JSONObject

首先,您应该将字符串转换为JSONObject,然后您可以使用关键字从JSONObject中获得JSONArray。下面的链接是关于这个的。

希望这能有所帮助。