我想要显示3条数据。所有3都是数据数组。
String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtPayment = new String[10];
我在TableLayout中尝试了以下内容:
public class DebtList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
SharedPreferences sharedPref= getSharedPreferences("chaosdata", 0);
//Load Data
Bundle extras = getIntent().getExtras();
String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtRate = new String[10];
String[] debtPayment = new String[10];
TableLayout tl = (TableLayout) findViewById(R.id.debtListTableView);
TableRow tr = new TableRow(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
TextView tv3 = new TextView(this);
for (int i=0;i<10;i++)
{
TextView tv0 = new TextView(this);
if (debtName[i] == null && extras != null)
{
debtName[i] = extras.getString("debtName");
debtAmount[i] = extras.getString("debtAmount");
debtRate[i] = extras.getString("debtRate");
debtPayment[i] = extras.getString("debtPayment");
}
tv1.setText("" + debtName[i]);
tv2.setText("" + debtAmount[i]);
tv3.setText("" + debtPayment[i]);
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
trlp.span = 3;
tr.setLayoutParams(trlp);
tr.addView(tv0);
}
tl.addView(tr);
String str = "";
for (int i = 0;i<debtName.length; i++) {
str = str+debtName[i];
// Do not append comma at the end of last element
if(i<debtName.length-1){
str = str+",";
}
}
SharedPreferences.Editor editor= sharedPref.edit();
editor.putString("debtNames", str);
String str1 = "";
for (int i = 0;i<debtAmount.length; i++) {
str1 = str1+debtAmount[i];
// Do not append comma at the end of last element
if(i<debtAmount.length-1){
str1 = str1+",";
}
}
editor.putString("debtAmounts", str1);
String str2 = "";
for (int i = 0;i<debtRate.length; i++) {
str2 = str2+debtRate[i];
// Do not append comma at the end of last element
if(i<debtRate.length-1){
str2 = str2+",";
}
}
editor.putString("debtRates", str2);
String str3 = "";
for (int i = 0;i<debtPayment.length; i++) {
str3 = str2+debtPayment[i];
// Do not append comma at the end of last element
if(i<debtPayment.length-1){
str3 = str3+",";
}
}
editor.putString("debtRates", str3);
}
这是我当前的.XML文件
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/debtListTableView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textAdditionalAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Additional Payments = $" />
<TextView
android:id="@+id/dispAdditionalAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.00" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Name" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Amount" />
<TextView
android:id="@+id/textOriginalPayoffDate"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Payment" />
<TextView
android:id="@+id/dispOriginalPayoffDate"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Months Remaining" />
</TableRow>
此编辑版本不会引发错误 - 但是,它仍然没有显示数据。
答案 0 :(得分:1)
因为您在tv0
中添加了相同的对象tr.addView(tv0)
,现在第二次在循环tr
中尝试添加已经拥有父tr
个自身的同一对象。所以第二次tv0
已经有父tr
因此无法添加并抛出异常。
试试这个它会起作用并用你的代码替换
TableLayout tl = (TableLayout) findViewById(R.id.debtListTableView);
TableRow tr = new TableRow(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
TextView tv3 = new TextView(this);
if (debtName[i] == null && extras != null)
{
debtName[i] = extras.getString("debtName");
debtAmount[i] = extras.getString("debtAmount");
debtRate[i] = extras.getString("debtRate");
debtPayment[i] = extras.getString("debtPayment");
}
tv1.setText("" + debtName[i]);
tv2.setText("" + debtAmount[i]);
tv3.setText("" + debtPayment[i]);
tr.addView(t1);
tr.addView(t2);
tr.addView(t3);
tl.addView(tr);
for (int i=0;i<10;i++)
{
TableRow tr = new TableRow(this);
TextView tv0 = new TextView(this);
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
trlp.span = 3;
tr.setLayoutParams(trlp);
tv0.setText("row "+(i+1));
tr.addView(tv0);
tl.addView(tr);
}