darshan singh
darshan singh

Reputation: 1

the specified child already has a parent you must call removeview android textview table layout

hiii...guys...m new in android development..i got this exception plz help me.."
the specified child already has a parent you must call removeview ".
android textview table layout at txtAmount.for loop doesnt enter in txtAmount to show text..
it show my item name and rate ant my specified textview txtItemName and txtRate respectivly but it doesnt show amount text at txtAmount in for loop.. show following exception.. "the specified child already has a parent you must call removeview ".

public class ConfirmOrder extends Activity {
    TextView txtLength;
    int length,rate,amount;
    String name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.confirmorder);
        Intent intent = getIntent();
         length=intent.getIntExtra("LENGTH", 0);
         rate=intent.getIntExtra("Rate", 0);
         amount=intent.getIntExtra("Amount", 0);
         name=intent.getStringExtra("Name");
        txtLength.setText(String.valueOf(amount));
         Log.d("length",""+length);
         Log.d("name",""+name);
         Log.d("Rate",""+rate);
         Log.d("Amount",""+amount);
        try
         {
        TableLayout tv=(TableLayout) findViewById(R.id.table);
                tv.removeAllViewsInLayout();
                int flag=1;
                for(int i=0;i<=10;i++)
            {
        TableRow tr=new TableRow(ConfirmOrder.this);
                 tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                 if(flag==1)
                 {
                   TextView Name=new TextView(ConfirmOrder.this);
                   Name.setPadding(10, 0, 0, 0);
                   Name.setTextSize(20);
                   Name.setText("ItemName");
                   Name.setTextColor(Color.WHITE);
                   tr.addView(Name);
                   TextView Rate=new TextView(ConfirmOrder.this);
                   Rate.setPadding(10, 0, 0, 0);
                   Rate.setTextSize(20);
                   Rate.setText("Rate");
                   Rate.setTextColor(Color.WHITE);
                   tr.addView(Rate);
                   TextView Amount=new TextView(ConfirmOrder.this);
                   Amount.setPadding(10, 0, 0, 0);
                   Amount.setTextSize(20);
                   Amount.setText("Amount");
                   Amount.setTextColor(Color.WHITE);
                   tr.addView(Amount);
                   tv.addView(tr);

                   final View vline = new View(ConfirmOrder.this);
                   vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
                   vline.setBackgroundColor(Color.GRAY);
                   tv.addView(vline);
                   flag=0;
                  }
                else
                 {
                   int l;
                   TextView[] txtItemName=new TextView[10];
                   TextView[] txtRate=new TextView[10];
                   TextView[] txtAmount=new TextView[10];
                   for( l=0; l<=10; l++)
                   {
                    txtItemName[l] = new TextView(this);
                    txtItemName[l].setTextSize(15);
                    txtItemName[l].setId(l);
                    txtItemName[l].setText(name);
                       tr.addView(txtItemName[l]);
                       tv.addView(tr);

                       txtRate[l] = new TextView(this);
                       txtRate[l].setTextSize(15);
                       txtRate[l].setId(l);
                       txtRate[l].setText(String.valueOf(rate));
                       tr.addView(txtRate[l]);
                       tv.addView(tr);

                       txtAmount[l] = new TextView(this);
                       txtAmount[l].setTextSize(15);
                       txtAmount[l].setId(l);
                     txtAmount[l].setText(String.valueOf(amount));
                       tr.addView(txtAmount[l]);
                      tv.addView(tr);


                   }                   
                   final View vline1 = new View(ConfirmOrder.this);
                   vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                   vline1.setBackgroundColor(Color.WHITE);
                   tv.addView(vline1); 
               }

               }

         }
         catch(Exception e)
         {
                 Log.e("log_tag", "Error parsing data "+e.toString());
                System.out.println("ERROR : " + e.getMessage());
                Log.d("Message",""+e.getMessage());

         }

    }   
}

Upvotes: 0

Views: 2305

Answers (1)

Gaurav Arora
Gaurav Arora

Reputation: 1815

From the code, the exception should be occuring here...

tv.addView(tr);

Since you are adding the same row again to the table Layout. Any view can be added to only one Parent only. If you want to change the parent, you'd need to remove the view from current parent and then add to other parent, as needed.

Upvotes: 4

Related Questions