【问题标题】:How to add a scroll View如何添加滚动视图
【发布时间】:2017-02-22 14:58:59
【问题描述】:

我想在 onClick 中向 tablerow 添加一个滚动视图我已经有代码但是它崩溃了不幸的是,已经停止了

java.lang.IllegalStateException: 指定的孩子已经有一个 父母。您必须首先在孩子的父母上调用 removeView()

当我单击 GridView 的项目时,我刚刚创建了一个 GridView,它显示了这个带有行的表格布局,这是我的 onClick 代码

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

【问题讨论】:

    标签: java android


    【解决方案1】:

    编辑:请注意,此答案是针对此处提出的原始问题(重新删除表格行)....问题已被编辑。

    当您向与之关联的表集标记添加行时

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

    然后您可以执行以下操作来删除该行。

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

    【讨论】:

    • @MahmoudAl-Harooney 您可以在上面编辑您的问题以显示您正在执行此操作的代码(或者添加新问题可能会更好)
    • 我认为添加新问题可能会更好(或者至少将您的问题附加到原始问题而不是完全替换它)....已接受的答案不再与所显示的问题有关)。
    • 我添加了一个新问题你可以检查一下
    猜你喜欢
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多