我想创建自定义对话框与表视图,但对话框不显示当我点击按钮。这是在按钮的onClick侦听器.....中调用的方法
public void dialogTable(){
int i;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = LayoutInflater.from(this);
View alertView = inflater.inflate(R.layout.table_dialog, null);
builder.setView(alertView);
TableLayout tableLayout = (TableLayout)alertView.findViewById(R.id.tableLayout);
TransactionDetails transactionDetails = new TransactionDetails(); //POJO class
for( i=0; i < 4; i++ ){
TableRow tableRow = new TableRow(getBaseContext());
tableRow.setLayoutParams(new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView textView1 = new TextView(getBaseContext());
textView1.setText(transactionDetails.getCustomerNo());
tableRow.addView(textView1);
TextView textView2 = new TextView(getBaseContext());
textView2.setText(transactionDetails.getAmount().toString());
tableRow.addView(textView2);
TextView textView3 = new TextView(getBaseContext());
textView3.setText(transactionDetails.getStatus());
tableRow.addView(textView3);
tableLayout.addView(tableRow);
}
builder.setCancelable(true);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
我认为这个错误是你使用了错误的上下文来膨胀你的布局。您必须使用构建器上下文作为文档:
您必须使用相同的上下文来创建小部件。下面的代码是:
public void dialogTable(View view)
{
int i;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Context dialogContext = builder.getContext();
LayoutInflater inflater = LayoutInflater.from(dialogContext);
View alertView = inflater.inflate(R.layout.table_dialog, null);
builder.setView(alertView);
TableLayout tableLayout = (TableLayout)alertView.findViewById(R.id.tableLayout);
TransactionDetails transactionDetails = new TransactionDetails();
for( i=0; i < 4; i++ ){
TableRow tableRow = new TableRow(dialogContext);
tableRow.setLayoutParams(new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView textView1 = new TextView(dialogContext);
textView1.setText(transactionDetails.getCustomerNo());
tableRow.addView(textView1);
TextView textView2 = new TextView(dialogContext);
textView2.setText(transactionDetails.getAmount().toString());
tableRow.addView(textView2);
TextView textView3 = new TextView(dialogContext);
textView3.setText(transactionDetails.getStatus());
tableRow.addView(textView3);
tableLayout.addView(tableRow);
}
builder.setCancelable(true);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
首先你应该检查你的按钮的监听器是否正确,其次你应该尝试设置TextView的文本颜色。
textView.setColor(Color.BLACK);