0

我有 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>

此编辑版本不会引发错误 - 但是,它仍然不显示数据。

4

1 回答 1

1

因为您正在添加相同的对象tv0,所以tr.addView(tv0)现在第二次在循环中tr尝试添加已经具有父trself 的相同对象。所以第二次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);
}
于 2013-11-03T06:38:27.753 回答