Reputation: 5591
I am retrieving information from server and trying to display it in a tabular format,hence using json object.
Code :
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);
}
After searching for a solution i found here but i don't know where to use this line.I am not getting about which child the exception is!It would be a great help if you can explain this.
Question: I know from the above link that the following line is the solution for my problem.
((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout);
But in my case where,how and why to use this?
Upvotes: 0
Views: 420
Reputation: 23234
In the code of TextViews b3, b4 and b5 you are trying to do tr.addView(b2);
so b2 is added 4 times in total. this should be b3, b4 and b5
On a sidenote, try to learn how to read crash logs. The Exception should state exactly on what line of code the error is on, so probably on the second occurence of tr.addView(b2);
Upvotes: 2