在Android中动态填充表格内容的问题

时间:2013-01-17 12:14:26

标签: android android-tablelayout

我的Android版本是:4.2

我从json动态填充表格布局。我已经尝试了以下代码。但它没有向我显示任何内容。我的Xml包含scrollview,表格布局和TextView的。

我的.java代码是:

private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>>{

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
       InputStream in = entity.getContent();
         StringBuffer out = new StringBuffer();
         int n = 1;
         while (n>0) {
             byte[] b = new byte[4096];
             n =  in.read(b);
             if (n>0) out.append(new String(b, 0, n));
         }
         return out.toString();

    }

    protected ArrayList<String> doInBackground(Void... params) {
         HttpClient httpClient = new DefaultHttpClient();
         HttpContext localContext = new BasicHttpContext();
         HttpGet httpGet = new HttpGet("my URL");

         String text = null;
         String result = null;
         ArrayList<String>  title= new ArrayList<String>();
         String url="";
         String desc="";

         try {
               //HttpResponse response = httpClient.execute(httpGet, localContext);
               HttpResponse response = httpClient.execute(httpGet, localContext);
               HttpEntity entity = response.getEntity();

                   text = getASCIIContentFromEntity(entity);

                   JSONArray ja = new JSONArray(text) ;

                                int n = ja.length();
                                for (int i = 0; i < n; i++) {

                                 JSONObject jo = ja.getJSONObject(i);

                                 title.add(jo.getString("Title"));

                                 url= jo.getString("URL");
                                 desc= jo.getString("Description");    


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


         return title;

    }   

    protected void onPostExecute(ArrayList<String> results) {
        if (results!=null) {

            Table=(TableLayout)findViewById(R.id.Table);
            TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams 
    (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
                //fill the table with Table rows dynamicly
        for(int i=0;i<results.size();i++){


            TableRow TR= new TableRow(MainActivity.this);
            TR.setLayoutParams(tableRowParams);
            Table.addView(TR);

            TextView tv1 =(TextView)findViewById(R.id.textView1);

            TR.addView(tv1);

            Object objPlace[] = new Object[results.size()] ;
            objPlace = results.toArray();

            tv1.setText("");

            for (int j = 0; j < results.size(); j++){

                tv1.append("DATA:\n" + results.get(j) + "\n");

            }
            tv1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
            }
        }

    }



}

Log Cat是:

01-17 16:44:39.871: D/AndroidRuntime(292): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-17 16:44:39.871: D/AndroidRuntime(292): CheckJNI is ON
01-17 16:44:40.011: D/AndroidRuntime(292): --- registering native functions ---
01-17 16:44:40.791: D/dalvikvm(181): GC_EXPLICIT freed 404 objects / 23872 bytes in 128ms
01-17 16:44:41.335: D/PackageParser(59): Scanning package: /data/app/vmdl48147.tmp
01-17 16:44:41.611: I/PackageManager(59): Removing non-system package:com.example.tabledemo
01-17 16:44:41.611: I/ActivityManager(59): Force stopping package com.example.tabledemo uid=10047
01-17 16:44:41.630: I/Process(59): Sending signal. PID: 279 SIG: 9
01-17 16:44:41.671: I/UsageStats(59): Unexpected resume of com.android.launcher while already resumed in com.example.tabledemo
01-17 16:44:42.510: I/WindowManager(59): WIN DEATH: Window{44015c98 com.example.tabledemo/com.example.tabledemo.MainActivity paused=false}
01-17 16:44:42.743: D/PackageManager(59): Scanning package com.example.tabledemo
01-17 16:44:42.751: I/PackageManager(59): Package com.example.tabledemo codePath changed from /data/app/com.example.tabledemo-2.apk to /data/app/com.example.tabledemo-1.apk; Retaining data and using new
01-17 16:44:42.763: I/PackageManager(59): /data/app/com.example.tabledemo-1.apk changed; unpacking
01-17 16:44:42.781: D/installd(35): DexInv: --- BEGIN '/data/app/com.example.tabledemo-1.apk' ---
01-17 16:44:44.480: D/dalvikvm(299): DexOpt: load 212ms, verify 887ms, opt 38ms
01-17 16:44:44.541: D/installd(35): DexInv: --- END '/data/app/com.example.tabledemo-1.apk' (success) ---
01-17 16:44:44.551: W/PackageManager(59): Code path for pkg : com.example.tabledemo changing from /data/app/com.example.tabledemo-2.apk to /data/app/com.example.tabledemo-1.apk
01-17 16:44:44.551: W/PackageManager(59): Resource path for pkg : com.example.tabledemo changing from /data/app/com.example.tabledemo-2.apk to /data/app/com.example.tabledemo-1.apk
01-17 16:44:44.551: D/PackageManager(59):   Activities: com.example.tabledemo.MainActivity
01-17 16:44:44.561: I/ActivityManager(59): Force stopping package com.example.tabledemo uid=10047
01-17 16:44:44.801: I/installd(35): move /data/dalvik-cache/data@app@com.example.tabledemo-1.apk@classes.dex -> /data/dalvik-cache/data@app@com.example.tabledemo-1.apk@classes.dex
01-17 16:44:44.810: D/PackageManager(59): New package installed in /data/app/com.example.tabledemo-1.apk
01-17 16:44:45.111: I/ActivityManager(59): Force stopping package com.example.tabledemo uid=10047
01-17 16:44:45.302: D/dalvikvm(134): GC_EXPLICIT freed 1232 objects / 66128 bytes in 183ms
01-17 16:44:45.400: D/dalvikvm(59): GC_EXPLICIT freed 12477 objects / 756648 bytes in 244ms
01-17 16:44:45.760: W/RecognitionManagerService(59): no available voice recognition services found
01-17 16:44:46.180: D/dalvikvm(59): GC_EXPLICIT freed 4929 objects / 272208 bytes in 147ms
01-17 16:44:46.231: D/dalvikvm(164): GC_EXPLICIT freed 2137 objects / 108864 bytes in 716ms
01-17 16:44:46.252: I/installd(35): unlink /data/dalvik-cache/data@app@com.example.tabledemo-2.apk@classes.dex
01-17 16:44:46.270: D/AndroidRuntime(292): Shutting down VM
01-17 16:44:46.280: D/dalvikvm(292): Debugger has detached; object registry had 1 entries
01-17 16:44:46.320: I/dalvikvm(292): JNI: AttachCurrentThread (from ???.???)
01-17 16:44:46.320: I/AndroidRuntime(292): NOTE: attach of thread 'Binder Thread #3' failed
01-17 16:44:47.055: D/AndroidRuntime(305): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-17 16:44:47.055: D/AndroidRuntime(305): CheckJNI is ON
01-17 16:44:47.211: D/AndroidRuntime(305): --- registering native functions ---
01-17 16:44:48.061: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.tabledemo/.MainActivity }
01-17 16:44:48.110: I/ActivityManager(59): Start proc com.example.tabledemo for activity com.example.tabledemo/.MainActivity: pid=311 uid=10047 gids={}
01-17 16:44:48.121: D/AndroidRuntime(305): Shutting down VM
01-17 16:44:48.131: D/dalvikvm(305): Debugger has detached; object registry had 1 entries
01-17 16:44:48.190: I/AndroidRuntime(305): NOTE: attach of thread 'Binder Thread #3' failed
01-17 16:44:49.381: I/ActivityManager(59): Displayed activity com.example.tabledemo/.MainActivity: 1275 ms (total 1275 ms)
01-17 16:44:54.530: D/dalvikvm(134): GC_EXPLICIT freed 899 objects / 51256 bytes in 124ms
01-17 16:44:59.481: D/dalvikvm(181): GC_EXPLICIT freed 242 objects / 15912 bytes in 64ms
01-17 16:45:05.183: D/dalvikvm(262): GC_EXPLICIT freed 757 objects / 54904 bytes in 146ms
01-17 16:48:06.281: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 16:53:06.294: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 16:58:06.344: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 17:03:06.383: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 17:08:06.421: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 17:13:06.452: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 17:18:06.482: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 17:23:06.547: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 17:28:06.602: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
01-17 17:33:06.630: D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol

任何人都会帮助我。

1 个答案:

答案 0 :(得分:0)

你为你的json评估互联网吗?在我看来,它不是表视图的问题。

request time failed: java.net.SocketException: Address family not supported by protocol

表示您的请求失败。尝试在 onPostExecute 中放置一个断点来检查结果是否正常。