简体   繁体   中英

How to add a scroll View

I want to add a scroll view to tablerow in onClick I already have the code but it crashes Unfortunately,has stopped

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

I Just Created a GridView when i click the item of GridView it show this table layout with rows and this is my onClick Code

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
      // TODO Auto-generated method stub
      //Result tr
      String st = "insert into ChecksItems (Check_ID,Item_ID,QTY,UnitPrice,TotalPrice,DicountValue,Tax_Value,Adj_Value,NetPrice,Serial,Fired,Fired_Time,Voided,Voided_Time,Voided_Reason,P_On_Check,Complement,Status,IsModifier,Ref_Mod_Item,IsAssimbly,Ref_Ass_Item,Taxable,NoServiceCharge,Num_Fired,Num_Print,Server_ID,P_On_Report,Check_ID_Combine,Round_Check_Fired,Void_Effect_Invn,Promo_ID,Orig_Price,Officer,Comp_Reason_ID,End_Serial_Count,Discount_ID,Disc_Reason_ID,Hold,Hold_Time,Voided_By,Comp_By,Disc_By) 
                   values (" + String.valueOf(MenuListView.Check_ID) + "," + alphabets.get(arg2).get("Item_ID").toString() + ",1," + alphabets.get(arg2).get("Price").toString() + "," + alphabets.get(arg2).get("Price").toString() + ",0,0,0," + alphabets.get(arg2).get("Price").toString() + "," + String.valueOf(MenuListView.Item_Serial) + ",0,GetDate(),0,GetDate(),0,'" + alphabets.get(arg2).get("PrintOnChick").toString() + "',0,'New',0,0,0,0,'" + alphabets.get(arg2).get("Taxable").toString() + "','" + alphabets.get(arg2).get("NoServiceCharge").toString() + "',0,0,0,'" + alphabets.get(arg2).get("PrintOnReport").toString() + "',0,1,0,0," + alphabets.get(arg2).get("Price").toString() + ",0,0,0,0,0,0,GetDate(),0,0,0)";

      if (ConnectionClass.executeUpdate(st)) {
          MenuListView.Item_Serial++;
          final TableRow tr_head2 = new TableRow(GridDynamic.this);
          tr_head2.setId(10);
          tr_head2.setBackgroundDrawable(getResources().getDrawable(R.drawable.fab_label_background));
          tr_head2.setPadding(10, 10, 10, 10);
          tr_head2.setLayoutParams(new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.FILL_PARENT,
                        TableLayout.LayoutParams.FILL_PARENT));

          TextView label_category2 = new TextView(GridDynamic.this);
          label_category2.setId(20);
          label_category2.setText("1");
          label_category2.setTextColor(Color.BLACK);
          label_category2.setTypeface(null, Typeface.BOLD_ITALIC);
          label_category2.setTextSize(18);
          label_category2.setPadding(20, 3, 20, 3);
          tr_head2.addView(label_category2);// add the column to the table row here

          TextView label_quantity_amount2 = new TextView(GridDynamic.this);
          label_quantity_amount2.setId(21);// define id that must be unique
          label_quantity_amount2.setText(alphabets.get(arg2).get("Name").toString()); // set the text for the header
          label_quantity_amount2.setTextColor(Color.BLACK); // set the color
          label_quantity_amount2.setTypeface(null, Typeface.BOLD_ITALIC);
          label_quantity_amount2.setTextSize(18);
          label_quantity_amount2.setPadding(20, 3, 20, 3); // set the padding (if required)
          tr_head2.addView(label_quantity_amount2); // add the column to the table row here

          TextView label_total_price_amount2 = new TextView(GridDynamic.this);
          label_total_price_amount2.setId(22);// define id that must be unique
          label_total_price_amount2.setText(alphabets.get(arg2).get("Price").toString()); // set the text for the header
          label_total_price_amount2.setTextColor(Color.BLACK); // set the color
          label_total_price_amount2.setTypeface(null, Typeface.BOLD_ITALIC);
          label_total_price_amount2.setTextSize(18);
          label_total_price_amount2.setPadding(20, 3, 20, 3); // set the padding (if required)
          tr_head2.addView(label_total_price_amount2); // add the column to the table row here

          tableLayout.addView(tr_head2, new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.FILL_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));

       ScrollView scroll = new ScrollView(GridDynamic.this);
       scroll.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                    TableRow.LayoutParams.FILL_PARENT));
       scroll.addView(tr_head2);

EDIT : note that this answer was for original question asked here (re. deleting table row)....question has since been edited.

When you're adding row to table set tag associated with it

        tableRow.setTag(<some key associated with this row>);
        tableLayout.addView(tableRow);

Then you can do following to delete the row.

        View view = tableLayout.findViewWithTag(key);
        if (view != null) {
            tableLayout.removeView(view);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM