如何在onClick

时间:2017-02-14 13:34:04

标签: java android

我创建了一个表行,如果我单击网格项,它会显示此表行,我需要此表行Scrollable。

我想在表格行的代码中添加动态滚动视图,但是当我使用onclick时它会崩溃,任何人都可以帮我解决这个错误

  

" java.lang.IllegalStateException:指定的子节点已经有了   家长。您必须先在孩子的父母身上调用removeView()。"

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // TODO Auto-generated method stub
                //Result tr
                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);

                Toast.makeText(getBaseContext(), alphabets.get(arg2).get("Item_ID").toString()+
                                " "+alphabets.get(arg2).get("Name").toString()+
                        " "+alphabets.get(arg2).get("Price").toString(),
                        Toast.LENGTH_SHORT).show();
            }
        });

1 个答案:

答案 0 :(得分:0)

您的代码中似乎有两个addView具有相同的参数tr_head2,这可能是原因。