Android在对话框中显示选定的表行

时间:2014-07-12 07:25:59

标签: android

我点击时试图在对话框中显示表格行。行包含图像。可以在对话框中查看吗?

当我在下面运行此代码时,它在LogCat中显示错误说" s"您必须首先在孩子的父母身上调用removeView()"。

错误就在这一行 - > " alertDialog2.setView(TR); "

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main);      
       TableLayout tbl_add = (TableLayout) findViewById(R.id.tableLayout1);


        int tr_size = tbl_add.getChildCount();
        for(int ctr = 0; ctr < tr_size; ctr ++){
        final TableRow tr = (TableRow)tbl_add.getChildAt(ctr);

            int td_size = tr.getChildCount();
                for(int inner_ctr = 0; inner_ctr < td_size; inner_ctr++){
                    tr.getChildAt(inner_ctr).setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) {
        AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
        alertDialog2.setView(tr);
        alertDialog2.show();
    }
    });
    }
  }
}

1 个答案:

答案 0 :(得分:0)

在新父级中设置视图之前,您需要将其从当前父视图中删除,如下所示

AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
//(tr.getParent()).removeView(tr)
((ViewGroup)tr.getParent()).removeView(tr);
alertDialog2.setView(tr);
alertDialog2.show();

然后它会起作用