I have a JSON string returned by my web service as follows:
{"checkrecord" [{"rollno": "abc2", "percent": 40, "participation": 12, "missed": 34}], "Table1": []}
The above line is my dataset. I have converted String to a JSON object, and now I want to put the data in a String in a TableLayout.
I want the table to look like the one shown on the Android app:
rollno percentage attended missed
abc2 40 12 34
This is the code I'm currently using:
public void jsonprocessing(String c)
{
try
{
JSONObject jsonobject = new JSONObject(c);
JSONArray array = jsonobject.getJSONArray("checkrecord");
int max = array.length();
for (int j = 0; j < max; j++)
{
JSONObject obj = array.getJSONObject(j);
JSONArray names = obj.names();
for (int k = 0; k < names.length(); k++)
{
name = names.getString(k);
value= obj.getString(name);
createtableLayout();
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
public void createtableLayout()
{
Log.d("values",value);
TableLayout t= (TableLayout)findViewById(R.id.tablelayout);
TableRow r1=new TableRow(this);
r1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView t1 = new TextView(this);
t1.setText(value);
t1.setTextColor(Color.BLACK);
t1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
r1.addView(t1);
t.addView(r1, new TableLayout.LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
My TableLayout contains a button and EditText as the first TableRow. After I click the button, I get a JSON string. Therefore, I need to insert 2 TableRows dynamically then according to my requirement.
xml :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tablelayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow>
<EditText
android:id="@+id/edittext"
android:width="200px" />
<Button
android:id="@+id/button"
android:text="Check Record"/>
</TableRow>
</TableLayout>
, , 2 , String?
. .
- ?