如何通过android中的表行将textviews的动态数组添加到tablelayout中? [英] how to add dynamic array of textviews to a tablelayout through table row in android?
问题描述
我一直试图通过表行在表布局中添加文本视图数组,但是该应用程序停止运行.
I have been trying to add array of text views in a table layout through table rows but the app stops working .
当我尝试在其他应用程序中执行相同的操作时,它可以工作,但是当我通过JSON提取数据时,在我当前的应用程序中实现它时,它不会动态创建文本视图.
when I try doing the same in other app it works but when I implement it it my current app when I m fetching the data through JSON, it doesn't create textviews dynamically.
下面是我的代码:
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是:私有静态最终字符串TAG_a =菜单";
here TAG_a is : private static final String TAG_a = "menu";
此处TAG_Name为:私有静态最终字符串TAG_Name =名称";
here TAG_Name is : private static final String TAG_Name = "Name";
itemList是:ArrayList> itemList;
itemList is : ArrayList> itemList;
我用作api的网址是:" http://ashapurasoftech.com/train/test .json ".
the url I m using as an api is : "http://ashapurasoftech.com/train/test.json".
我在我的代码中保留了一个日志,以便我可以知道类别字符串中包含的内容.现在,当我运行logcat时,它会显示:"d"各种饮料
I have kept a log in my code so as I can know what comes in my category string. Now when I run the logcat it displays :"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)
推荐答案
您的异常很有意义,AsyncTask不能从doInBackground执行UI操作.您必须publishProgress()并传递所需的任何参数.这会导致您的AsyncTask用您的参数在OnProgressUpdate中被调用,但是在此方法中,您位于UI线程上并且可以执行诸如addView()之类的操作.
Your exception makes sense, an AsyncTask must not do UI operations from doInBackground. You have to publishProgress() and pass whatever parameters you want; this results in your AsyncTask getting called in OnProgressUpdate with your parameters, but in this method you are on UI thread and can do operations like addView().
这篇关于如何通过android中的表行将textviews的动态数组添加到tablelayout中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!