0

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.

jpgerb
  • 1,043
  • 1
  • 9
  • 36

1 Answers1

1

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);
}
Hardik
  • 17,179
  • 2
  • 35
  • 40
  • This removed the errors; however, it still doesn't display anything. – jpgerb Nov 03 '13 at 06:51
  • did you setData to tv0? just do it tv0.setText("hello"); before tr.addView(tv0); – Hardik Nov 03 '13 at 06:54
  • I just added tv0.setText("hello"); prior to the addView(tv0) portion and nothing changes – jpgerb Nov 03 '13 at 06:56
  • Nothing is displaying still. I did test my variables to ensure they actually have data, and they do - so there is SOMETHING to display. It just doesn't want to display it.. – jpgerb Nov 03 '13 at 07:33
  • what you want to do in one row you need 10 textview? – Hardik Nov 03 '13 at 07:39
  • then you need to modify you code you are currently adding all data in one row – Hardik Nov 03 '13 at 07:49
  • I'm sorry for the delayed acceptance. This worked perfectly for what I asked. I just need to get the param to evenly distribute the weight. Thank you so much – jpgerb Nov 03 '13 at 19:44