I'm trying to display data passed into a JSONArray of JSONObjects in a TableLayout. The problem is that I know all the JSONObjects are correct (printing out the contents of next_object.getString("id") to Log.d works) so it must be an issue with how I'm trying to present the data.
My output is nothing but the first row, which I generate statically in XML since it's just the headers of each column.
This method supposedly grabs the TableLayout, loops through all the JSONObjects and adds their data one row at a time. Unfortunately, this loop breaks and I'm not sure why (as it, it only runs once, but there are no exceptions thrown).
TableLayout table = (TableLayout) findViewById(R.id.accelerometer_table);
for (int i = 0; i < in.length(); i++) {
JSONObject nextData = in.getJSONObject(i);
TableRow row = new TableRow(this);
row.setPadding(20, 20, 20, 20);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView date = new TextView(this);
TextView xAxis = new TextView(this);
TextView yAxis = new TextView(this);
TextView zAxis = new TextView(this);
date.setText(nextData.getString("date"));
date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
date.setGravity(Gravity.CENTER);
xAxis.setText(nextData.getString("x_axis"));
xAxis.setGravity(Gravity.CENTER);
yAxis.setText(nextData.getString("y_axis"));
yAxis.setGravity(Gravity.CENTER);
zAxis.setText(nextData.getString("z_axis"));
zAxis.setGravity(Gravity.CENTER);
row.addView(date);
row.addView(xAxis);
row.addView(yAxis);
row.addView(zAxis);
table.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
And here's the XML for the fragment that contains the table:
<TableLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:id="@+id/accelerometer_table"
android:stretchColumns="0,1,2,3" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="@string/accelerometer_header_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:text="@string/accelerometer_header_x_axis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:text="@string/accelerometer_header_y_axis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:text="@string/accelerometer_header_z_axis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</TableRow>
</TableLayout>
The XML only contains the header row which is static. The rest should be dynamically generated by the Java code in the first part after this row since they're the actual data rows.
Thanks!
No comments:
Post a Comment