0
我從服務器檢索信息,並嘗試以表格格式顯示它,因此使用json對象。「指定的子項已經有父項。您必須先調用子項的父項的removeView()。」異常在android
代碼:
JSONArray jsonArray = new JSONArray(jsonResult);
TableLayout tv=(TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag=1;
for (int i = 0; i < jsonArray.length() ; i++) {
//JSONObject object1 = jsonArray.getJSONObject(i);
TableRow tr=new TableRow(viewtimetable.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if(flag==1)
{
TextView col1=new TextView(viewtimetable.this);
col1.setText("Day");
col1.setTextColor(Color.BLUE);
col1.setTextSize(15);
tr.addView(col1);
TextView col2=new TextView(viewtimetable.this);
col2.setPadding(10, 0, 0, 0);
col2.setTextSize(15);
col2.setText("7:30-9:10AM");
col2.setTextColor(Color.BLUE);
tr.addView(col2);
TextView col3=new TextView(viewtimetable.this);
col3.setPadding(10, 0, 0, 0);
col3.setText("9:20-11:00AM");
col3.setTextColor(Color.BLUE);
col3.setTextSize(15);
tr.addView(col3);
TextView col4=new TextView(viewtimetable.this);
col4.setPadding(10, 0, 0, 0);
col4.setText("11:10-12:50PM");
col4.setTextColor(Color.BLUE);
col4.setTextSize(15);
tr.addView(col4);
TextView col5=new TextView(viewtimetable.this);
col5.setPadding(10, 0, 0, 0);
col5.setText("1:40-3:20PM");
col5.setTextColor(Color.BLUE);
col5.setTextSize(15);
tr.addView(col5);
TextView col6=new TextView(viewtimetable.this);
col6.setPadding(10, 0, 0, 0);
col6.setText("3:30-5:00PM");
col6.setTextColor(Color.BLUE);
col6.setTextSize(15);
tr.addView(col6);
tv.addView(tr);
final View vline = new View(viewtimetable.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag=0;
}
else
{
try{
JSONObject json_data = jsonArray.getJSONObject(i);
//Log.i("log_tag","id: "+json_data.getInt("f1")+
// ", Username: "+json_data.getString("f2")+
// ", No: "+json_data.getInt("f3"));
//((ViewGroup)tr.getParent()).removeView(tr);
TextView b=new TextView(viewtimetable.this);
String stime=json_data.getString("day");
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1=new TextView(viewtimetable.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
if(json_data.getString("slot").equals("7:30-9:10AM")){
String stime1=json_data.getString("subject");
b1.setText(stime1);
}
b1.setTextColor(Color.WHITE);
tr.addView(b1);
TextView b2=new TextView(viewtimetable.this);
b2.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("9:20-11:00AM")){
String stime2=json_data.getString("subject");
b2.setText(stime2);
}
b2.setTextColor(Color.RED);
b2.setTextSize(15);
tr.addView(b2);
TextView b3=new TextView(viewtimetable.this);
b3.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("11:10-12:50PM")){
String stime3=json_data.getString("subject");
b3.setText(stime3);
}
b3.setTextColor(Color.RED);
b3.setTextSize(15);
tr.addView(b2);
TextView b4=new TextView(viewtimetable.this);
b4.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("1:40-3:20PM")){
String stime4=json_data.getString("subject");
b4.setText(stime4);
}
b4.setTextColor(Color.RED);
b4.setTextSize(15);
tr.addView(b2);
TextView b5=new TextView(viewtimetable.this);
b5.setPadding(10, 0, 0, 0);
if(json_data.getString("slot").equals("3:30-5:00PM")){
String stime5=json_data.getString("subject");
b5.setText(stime5);
}
b5.setTextColor(Color.RED);
b5.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
}catch(JSONException e){
content.setText("jsonexception");
}
final View vline1 = new View(viewtimetable.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
尋找解決的辦法,我發現here後,但我不知道在哪裏可以使用此line.I不是在獲取有關的例外是孩子這將是一個如果你能解釋這一點,那麼很有幫助
問題: 我知道從上面的鏈接,下面的行是我的問題的解決方案。
((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout);
但在我的情況下,如何以及爲什麼要使用它?
感謝您計算出我愚蠢而愚蠢的錯誤。並感謝您的建議。 –