当前位置: 动力学知识库 > 问答 > 编程问答 >

Android Display Selected Table Row in Dialog

问题描述:

I'm trying to display table row in dialog box when it clicked . The rows contains images. Is that possible to view in dialog ?

When I run this code below it shows an error in LogCat say's "You must call removeView() on the child's parent first".

and the error is on this line -> " 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();

}

});

}

}

}

网友答案:

Before setting a view in a new parent you need to remove it from current parent view as below

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

Then it will work

分享给朋友:
您可能感兴趣的文章:
随机阅读: