Reputation: 219
I am creating a custom dialog that will show the information of a food item: the xml layout for the dialog is
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/lblCal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/lblFat"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TextView
android:id="@+id/lblCarb"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:text=" OK "
android:id="@+id/btnOK"
android:layout_height="wrap_content">
</Button>
<Button android:layout_width="wrap_content"
android:text="Home"
android:id="@+id/btnHome"
android:layout_height="wrap_content">
</Button>
</TableRow>
</LinearLayout>
the onCreatDialog method is defined like this:
@Override
protected Dialog onCreateDialog(int id) {
Dialog d;
switch (id) {
case 0:
d = new Dialog(this);
d.setContentView(R.layout.result_dialog);
d.setTitle(item);
TextView lblEnergy = (TextView) d.findViewById(R.id.lblCal);
lblEnergy.setText("Energy: "+ String.valueOf(cal));
TextView lblFat = (TextView) d.findViewById(R.id.lblCal);
lblFat.setText("Fat: "+ String.valueOf(fat));
TextView lblCarbs = (TextView) d.findViewById(R.id.lblCal);
lblCarbs.setText("Carbs: " + String.valueOf(carb));
Button ok = (Button) d.findViewById(R.id.btnOK);
ok.setOnClickListener(i);
Button home = (Button) d.findViewById(R.id.btnHome);
home.setOnClickListener(i);
break;
default: d = null;
}
return d;
}
but when I run the program the first two textview don't appear.
what is the problem?!!
what I should add to the ok button to close the dialog, because when I call DissmisDialog(0); when I click again on another item the dialog contents never changed
Upvotes: 0
Views: 310
Reputation: 8942
You need to use the correct ids for the findViewById
calls:
Old:
TextView lblEnergy = (TextView) d.findViewById(R.id.lblCal);
lblEnergy.setText("Energy: "+ String.valueOf(cal));
TextView lblFat = (TextView) d.findViewById(R.id.lblCal);
lblFat.setText("Fat: "+ String.valueOf(fat));
TextView lblCarbs = (TextView) d.findViewById(R.id.lblCal);
lblCarbs.setText("Carbs: " + String.valueOf(carb));
New:
TextView lblEnergy = (TextView) d.findViewById(R.id.lblCal);
lblEnergy.setText("Energy: "+ String.valueOf(cal));
TextView lblFat = (TextView) d.findViewById(R.id.lblFat /* <<< */);
lblFat.setText("Fat: "+ String.valueOf(fat));
TextView lblCarbs = (TextView) d.findViewById(R.id.lblCarb /* <<< */);
lblCarbs.setText("Carbs: " + String.valueOf(carb));
Upvotes: 2