I want to make a table that have to call my data from my database using JSON and volley, so i need to loop it. In my loop i have a few textview and i use addView
to add it to the row.The addView
was works well if i only have one row data in my database but it will get an error if i have more then one. The error is
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
.
So, i think its get an error because the loop was call a same addView
.
Sorry, I'm not too fluent in English :)
Here is my code:
public class DaftarNilaiUtama3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daftar_nilai_utama3);
final TableLayout tableview = (TableLayout) findViewById(R.id.tableview);
final TableRow row = new TableRow(this);
final TextView tvModul = new TextView(this);
final TextView tvRes = new TextView(this);
final TextView tvPost = new TextView(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5,5,0,5);
row.setLayoutParams(lp);
Response.Listener responseListener= new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
List<String> item = new ArrayList<String>();
JSONArray jsonMainNode = new JSONArray(response);
JSONObject jsonResponse = new JSONObject();
for (int i = 0; i < jsonMainNode.length(); i++) {
jsonResponse = jsonMainNode.getJSONObject(i);
String nama_modul = jsonResponse.optString("nama_modul");
String responsi_nilai = jsonResponse.optString("responsi_nilai");
String posttest_nilai = jsonResponse.optString("posttest_nilai");
tvModul.setGravity(Gravity.CENTER);
tvModul.setTextSize(12.0f);
tvModul.setBackgroundColor(Color.parseColor("#eeeeee"));
tvModul.setTypeface(null, Typeface.BOLD);
tvModul.setText(nama_modul);
row.addView(tvModul);
tvRes.setGravity(Gravity.CENTER);
tvRes.setTextSize(12.0f);
tvRes.setBackgroundColor(Color.parseColor("#eeeeee"));
tvRes.setTypeface(null, Typeface.BOLD);
tvRes.setText(responsi_nilai);
row.addView(tvRes);
tvPost.setGravity(Gravity.CENTER);
tvPost.setTextSize(12.0f);
tvPost.setBackgroundColor(Color.parseColor("#eeeeee"));
tvPost.setTypeface(null, Typeface.BOLD);
tvPost.setText(posttest_nilai);
row.addView(tvPost);
tableview.addView(row);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RequestTableDNU3 lvReq = new RequestTableDNU3(nama_klp,nama_mhs, responseListener);
RequestQueue queue = Volley.newRequestQueue(DaftarNilaiUtama3.this);
queue.add(lvReq);
}
}
Try this..You have to create table row for each iteration in loop and then have to add to table layout. add context as DaftarNilaiUtama3.this
for (int i = 0; i < jsonMainNode.length(); i++) {
final TableRow row = new TableRow(DaftarNilaiUtama3 .this);
jsonResponse = jsonMainNode.getJSONObject(i);
String nama_modul = jsonResponse.optString("nama_modul");
String responsi_nilai = jsonResponse.optString("responsi_nilai");
String posttest_nilai = jsonResponse.optString("posttest_nilai");
tvModul.setGravity(Gravity.CENTER);
tvModul.setTextSize(12.0f);
tvModul.setBackgroundColor(Color.parseColor("#eeeeee"));
tvModul.setTypeface(null, Typeface.BOLD);
tvModul.setText(nama_modul);
row.addView(tvModul);
tvRes.setGravity(Gravity.CENTER);
tvRes.setTextSize(12.0f);
tvRes.setBackgroundColor(Color.parseColor("#eeeeee"));
tvRes.setTypeface(null, Typeface.BOLD);
tvRes.setText(responsi_nilai);
row.addView(tvRes);
tvPost.setGravity(Gravity.CENTER);
tvPost.setTextSize(12.0f);
tvPost.setBackgroundColor(Color.parseColor("#eeeeee"));
tvPost.setTypeface(null, Typeface.BOLD);
tvPost.setText(posttest_nilai);
row.addView(tvPost);
tableview.addView(row);
}