gpt4 book ai didi

android - 如何通过解析json数据动态添加table view

转载 作者:行者123 更新时间:2023-11-30 02:41:23 27 4
gpt4 key购买 nike

我正在将一个函数调用到一个线程中,并将这些对象放入 tableView 但我正在处理非法状态异常。我无法解决为什么?这是我负责调用该函数的代码:

 runOnUiThread(new Runnable() {

fillTable(jArray);

}
});

函数是:

private void fillTable(JSONArray jArray){

TextView t1v = new TextView(ReportAllActivity.this);
TextView t2v = new TextView(ReportAllActivity.this);
TextView t3v = new TextView(ReportAllActivity.this);
TextView t4v = new TextView(ReportAllActivity.this);
TextView t5v = new TextView(ReportAllActivity.this);
TextView t6v = new TextView(ReportAllActivity.this);
ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();
for(int i = 0 ; i < jArray.length() ; i++)
{
listdata.add(jArray.optJSONObject(i));

}
int i = jArray.length()-1;

while( i >= 0 )
{
try {
json_data = listdata.get(i);
//json_data = jArray.getJSONObject(i);
TableRow tbrow = new TableRow(ReportAllActivity.this);
t1v.setText(json_data.getString("heat"));
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
tbrow.addView(t1v);

t2v.setText(json_data.getString("oc_count"));
t2v.setTextColor(Color.WHITE);
t2v.setGravity(Gravity.CENTER);
tbrow.addView(t2v);

t3v.setText(json_data.getString("who"));
t3v.setTextColor(Color.WHITE);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);

t4v.setText(json_data.getString("alarm"));
t4v.setTextColor(Color.WHITE);
t4v.setGravity(Gravity.CENTER);
tbrow.addView(t4v);


t5v.setText(json_data.getString("do_duration"));
t5v.setTextColor(Color.WHITE);
t5v.setGravity(Gravity.CENTER);
tbrow.addView(t5v);


t6v.setText(json_data.getString("input_time"));
t6v.setTextColor(Color.WHITE);
t6v.setGravity(Gravity.CENTER_HORIZONTAL);
tbrow.addView(t6v);
listdata.remove(i);
i--;
reportTable.addView(tbrow);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
}

和我的 LogCat 文件:

    09-07 16:28:57.142: E/AndroidRuntime(696): FATAL EXCEPTION: main
09-07 16:28:57.142: E/AndroidRuntime(696): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-07 16:28:57.142: E/AndroidRuntime(696): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
09-07 16:28:57.142: E/AndroidRuntime(696): at android.view.ViewGroup.addView(ViewGroup.java:1865)
09-07 16:28:57.142: E/AndroidRuntime(696): at android.view.ViewGroup.addView(ViewGroup.java:1822)
09-07 16:28:57.142: E/AndroidRuntime(696): at android.view.ViewGroup.addView(ViewGroup.java:1802)
09-07 16:28:57.142: E/AndroidRuntime(696): at com.example.hsvarge.ReportAllActivity.fillTable(ReportAllActivity.java:263)
09-07 16:28:57.142: E/AndroidRuntime(696): at com.example.hsvarge.ReportAllActivity.access$1(ReportAllActivity.java:192)
09-07 16:28:57.142: E/AndroidRuntime(696): at com.example.hsvarge.ReportAllActivity$3$1.run(ReportAllActivity.java:170)
09-07 16:28:57.142: E/AndroidRuntime(696): at android.os.Handler.handleCallback(Handler.java:587)
09-07 16:28:57.142: E/AndroidRuntime(696): at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 16:28:57.142: E/AndroidRuntime(696): at android.os.Looper.loop(Looper.java:123)
09-07 16:28:57.142: E/AndroidRuntime(696): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 16:28:57.142: E/AndroidRuntime(696): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 16:28:57.142: E/AndroidRuntime(696): at java.lang.reflect.Method.invoke(Method.java:521)
09-07 16:28:57.142: E/AndroidRuntime(696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 16:28:57.142: E/AndroidRuntime(696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 16:28:57.142: E/AndroidRuntime(696): at dalvik.system.NativeStart.main(Native Method)

我该如何解决这个问题?提前致谢。

最佳答案

您的问题出现是因为到处都使用相同的 TextView。

tbrow.addView(t1v);

您将在每次迭代中添加此 View 。但这应该是不同的看法。您应该在算法的每次迭代中创建不同的 TextView。

关于android - 如何通过解析json数据动态添加table view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25712488/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com