I am trying to show a table of data in my custom alertdialog in my app.
My custom alert xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Page" />
<TextView
android:text="ENo" />
<TextView
android:text="QNo" />
<TextView
android:text="P_date" />
<TextView
android:text="P_read" />
<TextView
android:text="C_read" />
</TableRow>
<TableRow>
<ScrollView
android:layout_width="match_parent"
android:layout_height="450dp"
android:scrollbars="vertical"
android:layout_weight="1"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/meter_details"
android:stretchColumns="*">
</TableLayout>
</ScrollView>
</TableRow>
</TableLayout>
My java code of populating the child table (with id: meter_details):
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
AlertDialog ad;
builder.setIcon(R.drawable.ic_power);
builder.setTitle("Meter entry");
builder.setMessage("Please enter the details");
Context dialogContext = builder.getContext();
LayoutInflater inflater = LayoutInflater.from(dialogContext);
View dialogView = inflater.inflate(R.layout.meter_entry_dialog, null);
builder.setView(dialogView);
TableLayout tbl = dialogView.findViewById(R.id.meter_details);
for(int i=0;i<arr.size();i++){
TableRow row = new TableRow(getContext());
row.setBackground(getResources().getDrawable(R.drawable.border));
TextView pg = new TextView(getContext());
TextView empno = new TextView(getContext());
TextView qtrno = new TextView(getContext());
TextView prev_date = new TextView(getContext());
EditText prev_read = new EditText(getContext());
EditText curr_read = new EditText(getContext());
try {
pg.setText(arr.get(i).get(7).toString());
empno.setText(arr.get(i).get(1).toString());
qtrno.setText(arr.get(i).get(2).toString());
prev_date.setText(arr.get(i).get(5).toString());
prev_read.setText(arr.get(i).get(4).toString());
row.addView(pg);
row.addView(empno);
row.addView(qtrno);
row.addView(prev_date);
row.addView(prev_read);
row.addView(curr_read);
tbl.addView(row);
} catch (JSONException e) {
e.printStackTrace();
}
}
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setCancelable(false);
ad = builder.create();
ad.show();
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(ad.getWindow().getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
ad.getWindow().setAttributes(layoutParams);
Only the first row of my parent tablelayout shows in my alertdialog. The rest of my data (which I am fetching from array and dynamically creating table rows and storing them) doesn't show.
I have no idea as to why this is happening. I have even checked my arr, it has all the values which are required. Please help me
You should set the inflated dialogView as view for the dialog.
Change this line builder.setView(R.layout.meter_entry_dialog); to builder.setView(dialogView); and move it after dialogView's initialization.
Also, you can use the getContext() method for the layout inflater.
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
AlertDialog ad;
builder.setIcon(R.drawable.ic_power);
builder.setTitle("Meter entry");
builder.setMessage("Please enter the details");
LayoutInflater inflater = LayoutInflater.from(getContext());
View dialogView = inflater.inflate(R.layout.meter_entry_dialog, null);
builder.setView(dialogView);
TableLayout tbl = dialogView.findViewById(R.id.meter_details);
...