Question

I have 3 pieces of data I want to have displayed. All 3 are arrays of data.

String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtPayment = new String[10];

I attempted the following in my 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);
}

This is my current .XML file

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

This edited version does not throw an error - however, it is still not displaying data.

Was it helpful?

Solution

because you are adding same object tv0 in tr.addView(tv0), now second time in loop tr trying to add same object which have already parent tr self. so second time tv0 have already parent tr so it can not be added and throw exception.

try this it will work and replace with your code

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top