lundi 18 mai 2015

Android: The specified child already has a parent issue

I Found this Same question around 6 times in this site. and tried all the solutions. but no luck.
Here is My Code
MyActivity.java

public void displayTable(){//This is method to display table
        List<Integer> lstIds = helper.getAllTimeOffID(); // it will fetch number of records
        for(int id:lstIds){
            //for each record i am generationg single row(LinearLayout[horizontal])
            TimeOff timeOff = helper.getTimeOffInfo(id);//this is model class for each record
            lstAllTimeOff.add(timeOff); // adding to list for future purpose(no need for this mithod)
            int pxs = this.getApplicationContext().getResources().getDimensionPixelSize(R.dimen.generalTextSize);
            //////////////////////////////////////////
            LinearLayout viewTimeOffDetailsArea = (LinearLayout) this.findViewById(R.id.viewTimeOffDetailsArea);
            LinearLayout viewSingleRow = new LinearLayout(getApplicationContext()); //new row generating
            LayoutParams singleRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            viewSingleRow.setOrientation(LinearLayout.HORIZONTAL);
            TextView lblSingleRowDetail = new TextView(getApplicationContext());
            TextView lblSingleRowDate = new TextView(getApplicationContext());
            TextView lblSingleRowHours = new TextView(getApplicationContext());
            ImageButton imgbtnSingleRowAction = new ImageButton(getApplicationContext());
            ///////////////////////////////////////////////////////////////////////
            viewSingleRow.removeAllViews();
            lblSingleRowDetail.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 3f));
            lblSingleRowDate.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 2f));
            lblSingleRowHours.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
            lblSingleRowDetail.setTextSize(pxs);
            lblSingleRowDate.setTextSize(pxs);
            lblSingleRowHours.setTextSize(pxs);
            lblSingleRowDetail.setText(timeOff.getTimeOffDetail());
            lblSingleRowDate.setText(timeOff.getTimeOffDate());
            lblSingleRowHours.setText(timeOff.getTimeOffHours());
            imgbtnSingleRowAction.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            imgbtnSingleRowAction.setImageResource(R.drawable.delete1);
            imgbtnSingleRowAction.setId(timeOff.getId());

            viewSingleRow.addView(lblSingleRowDetail);//add label
            viewSingleRow.addView(lblSingleRowDate);//add label
            viewSingleRow.addView(lblSingleRowHours);//add label
            viewSingleRow.addView(imgbtnSingleRowAction);//add image button
            //add whole row into vertical Linear Layout which is in Scroll View
            viewTimeOffDetailsArea.addView(lblSingleRowDetail); //on this line i am getting Error

        }

and this is my xml file where i am trying to draw ma rows one by one
my_xml.xml

  <ScrollView
        android:id="@+id/scrollDiv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/viewTimeOffDetailsFooter"
        android:layout_below="@id/lowerSeparator" >

        <LinearLayout
            android:id="@+id/viewTimeOffDetailsArea"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >


        </LinearLayout>
    </ScrollView>

I am Getting This Error. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Aucun commentaire:

Enregistrer un commentaire