用json填充表格布局

时间:2016-03-07 07:49:40

标签: android json

我有一个Android应用程序应该解析php Web服务的json输出并以表格布局显示它 我尝试使用以下代码,但运行应用程序后没有显示数据 有人可以帮助PLZ

public class CreateTable extends Activity 
 {


     TableLayout tl;
     TableRow tr;
     TextView companyTV,valueTV;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tl = (TableLayout) findViewById(R.id.maintable);
        addData();
    }
     public void addData()
    {
        try 
        {
            String data;
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://192.168.1.10:89/.../Z.php");
            HttpResponse response = client.execute(request);
            HttpEntity entity=response.getEntity();
            data=EntityUtils.toString(entity);
            Log.e("STRING", data);



       JSONArray json=new JSONArray(data);

       for(int i=0;i<json.length(); i++)

       {

        JSONObject obj=json.getJSONObject(i);
        String name=obj.getString("Part_NAME").toString();
        String id = obj.getString("Part_ID").toString() ;
        Log.e("name", name);


            /** Create a TableRow dynamically **/
            tr = new TableRow(this);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            /** Creating a TextView to add to the row **/
            companyTV = new TextView(this);
            companyTV.setText(name);
            companyTV.setTextColor(Color.RED);
            companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            companyTV.setPadding(5, 5, 5, 5);
            tr.addView(companyTV);  // Adding textView to tablerow.

            /** Creating another textview **/
            valueTV = new TextView(this);
            valueTV.setText(id);
            valueTV.setTextColor(Color.GREEN);
            valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            valueTV.setPadding(5, 5, 5, 5);
            valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            tr.addView(valueTV); // Adding textView to tablerow.

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

}}  

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="none">
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0,1"
            android:id="@+id/maintable" >
        </TableLayout>
    </ScrollView>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

首先,在android开发中,网络操作应该在一个Thread而不是ui Thread。

在这个地方:

catch(Exception e){}

您可以打印错误以进行检查:

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