我正在使用android studio,我在片段中创建了动态表格布局,但是由于我的列在整个设置中没有设置相同的问题。对于测试我使用字符串作为json代码如下所示。将有4个对象将由此字符串创建,因此将使用8个textViews(1将为空白)创建布局,并将重复4次但数据显示为1.5 timw(wierd)
java file->
String alllist ="[{\"OrderID\":\"1\",\"userID\":\"mananpandya.sgvu@gmail.com\",\"Order_Given\":\"2015-10-13 11:12:25\",\"Services\":[\"Washing\",\"ironing\",\"woollen washing\",\"washing + folding\"],\"Schedule_Pickup\":\"11-10-15 10:00-1:00\",\"Schedule_Delivery\":\"13-10-15 10:00-1:00\",\"No_Of_Clothes\":\"15\",\"Weight_Clothes\":\"5kg\",\"Total_Amount\":\"400\",\"OrderStage\":\"4\"},{\"OrderID\":\"13\",\"userID\":\"mananpandya.sgvu@gmail.com\",\"Order_Given\":\"2015-10-13 11:16:25\",\"Schedule_Pickup\":\"12-10-15 10\",\"Schedule_Delivery\":\"12-15-10 12\",\"No_Of_Clothes\":\"20\",\"Weight_Clothes\":\"10kg\",\"Total_Amount\":\"250\",\"OrderStage\":\"1\"},{\"OrderID\":\"14\",\"userID\":\"mananpandya.sgvu@gmail.com\",\"Order_Given\":\"2015-10-13 11:16:28\",\"Schedule_Pickup\":\"12-10-15 10\",\"Schedule_Delivery\":\"12-15-10 12\",\"No_Of_Clothes\":\"22\",\"Weight_Clothes\":\"7kg\",\"Total_Amount\":\"750\",\"OrderStage\":\"3\"},{\"OrderID\":\"15\",\"userID\":\"mananpandya.sgvu@gmail.com\",\"Order_Given\":\"2015-10-13 11:16:28\",\"Schedule_Pickup\":\"12-10-15 10\",\"Schedule_Delivery\":\"12-15-10 12\",\"No_Of_Clothes\":\"22\",\"Weight_Clothes\":\"7kg\",\"Total_Amount\":\"750\",\"OrderStage\":\"2\"}]";
try {
JSONArray j1 = new JSONArray(alllist);
tl = (TableLayout) view.findViewById(R.id.allprevordertable);
tl.removeAllViews();
for (int z = 0; z < 2; z++) {
JSONObject job = j1.getJSONObject(z);
tr = new TableRow(getActivity());
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView ordername = new TextView(getActivity());
ordername.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
ordername.setTextColor(Color.parseColor("#FF52caf7"));
ordername.setText("Order " + job.getString("OrderID"));
tr.addView(ordername);
TextView trackorder = new TextView(getActivity());
trackorder.setText("track order");
trackorder.setTextColor(Color.parseColor("#FF52caf7"));
trackorder.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(trackorder);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr = new TableRow(getActivity());
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView pdate = new TextView(getActivity());
pdate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
pdate.setTextColor(Color.parseColor("#FF52caf7"));
pdate.setText("pickup: " + job.getString("Order_Given"));
tr.addView(pdate);
TextView blank = new TextView(getActivity());
blank.setText("");
blank.setTextColor(Color.parseColor("#FF52caf7"));
blank.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(blank);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr = new TableRow(getActivity());
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView stype = new TextView(getActivity());
stype.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
stype.setTextColor(Color.parseColor("#FF52caf7"));
stype.setText(job.getString("Services"));
tr.addView(stype);
TextView cost = new TextView(getActivity());
cost.setText("Rs." + job.getString("Total_Amount"));
cost.setTextColor(Color.parseColor("#FF52caf7"));
cost.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(cost);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr = new TableRow(getActivity());
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView weight = new TextView(getActivity());
weight.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
weight.setTextColor(Color.parseColor("#FF52caf7"));
weight.setText(job.getString("Weight_Clothes"));
tr.addView(weight);
TextView status = new TextView(getActivity());
status.setText(job.getString("OrderStage") );
status.setTextColor(Color.parseColor("#FF52caf7"));
status.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(status);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
} catch (Exception e) {
e.printStackTrace();
}
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/re/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/pbackground">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/allprevordertable"
android:stretchColumns="*">
</TableLayout>