如何在onClick中添加一个动态滚动视图

问题描述:

我创建了一个表格行,如果我点击网格项目,它会显示此表格行,我需要这个表格行可滚动。如何在onClick中添加一个动态滚动视图

我想在此代码在表行添加一个动态滚动视图,但它坠毁时我使用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(); 
      } 
     }); 

似乎有在使用相同的参数tr_head2,这可能是这个原因你的代码是两个addView