【问题标题】:how to add dynamic array of textviews to a tablelayout through table row in android?如何通过android中的表格行将动态的textview数组添加到tablelayout?
【发布时间】:2014-04-14 05:54:46
【问题描述】:

我一直在尝试通过表格行在表格布局中添加文本视图数组,但应用程序停止工作。

当我尝试在其他应用程序中执行相同操作时,它可以工作,但是当我通过 JSON 获取数据时将它实现为我当前的应用程序时,它不会动态创建文本视图。

下面是我的代码:

 protected Void doInBackground(Void... arg0) {

        ServiceHandler sh = new ServiceHandler();
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        if (jsonStr != null) {
            try {


                JSONObject jsonObj = new JSONObject(jsonStr);

                items = jsonObj.getJSONArray(TAG_a);

                    TextView[] tx = new TextView[items.length()];
                    TableRow[] tr = new TableRow[items.length()];

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

                    JSONObject c = items.getJSONObject(i);

                    String category = c.getString(TAG_Cat);
                    String name = c.getString(TAG_Name);

                    HashMap<String, String> item = new HashMap<String, String>();


                    tx[i] = new TextView(S1.this);
                    tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    tx[i].setText(category);

                    tr[i] = new TableRow(S1.this);
                    tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    tr[i].addView(tx[i],new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                    Log.d("d", category);

                    tl.addView(tr[i],new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));   

                    item.put(TAG_Name, name);
                    itemList.add(item);                         



                }

这里 TAG_a 是:private static final String TAG_a = "menu";

这里 TAG_Name 是:private static final String TAG_Name = "Name";

itemList is : ArrayList> itemList;

我用作 api 的网址是:“http://ashapurasoftech.com/train/test.json”。

我已经在我的代码中记录了日志,以便知道我的类别字符串中有什么。现在,当我运行 logcat 时,它会显示:"d" Assorted Beverages

我的 Logcat:

 04-14 03:20:56.247: E/AndroidRuntime(1133): FATAL EXCEPTION: AsyncTask #1
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at java.lang.Thread.run(Thread.java:841)
 04-14 03:20:56.247: E/AndroidRuntime(1133): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only  the original thread that created a view hierarchy can touch its views.
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6006)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:822)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.View.requestLayout(View.java:16392)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.View.requestLayout(View.java:16392)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.View.requestLayout(View.java:16392)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.View.requestLayout(View.java:16392)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.View.requestLayout(View.java:16392)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.widget.TableLayout.requestLayout(TableLayout.java:230)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.ViewGroup.addView(ViewGroup.java:3413)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.widget.TableLayout.addView(TableLayout.java:429)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.view.ViewGroup.addView(ViewGroup.java:3391)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.widget.TableLayout.addView(TableLayout.java:420)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at com.app.crossdine.S1$Getitems.doInBackground(S1.java:157)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at com.app.crossdine.S1$Getitems.doInBackground(S1.java:1)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
 04-14 03:20:56.247: E/AndroidRuntime(1133):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

【问题讨论】:

  • 您每次都通过循环重新创建数组?也许这些行属于循环之前:TextView[] tx = new TextView[items.length()];TableRow[] tr = new TableRow[items.length()];
  • 我更改了它,并将 textview 和 tableRow 放在 for 循环之外。 bt 它会在我停止工作时引发异常。查看我编辑的代码

标签: android json


【解决方案1】:

您的例外是有道理的,AsyncTask 不得从 doInBackground 执行 UI 操作。您必须 publishProgress() 并传递您想要的任何参数;这会导致在 OnProgressUpdate 中使用您的参数调用您的 AsyncTask,但在此方法中您位于 UI 线程上并且可以执行 addView() 之类的操作。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多