So, my current issue is that I can't find an elegant way to update a dialog box when a button is pressed. I can achieve functionally the same result by dismiss() and show(), but that is ugly.
Lets say this dialog has 3 buttons, for selling widgets that the player has. Sell All, Sell 10, and Sell X (amount entered with a EditText). I'd like for the dialog to persist if the player pushes Sell 10, but also to update it's textviews with the new count of widgets.
Pertinent part of the XML layout of the custom dialog:
<LinearLayout android:id="@+id/linearLayout3" android:layout_height="wrap_content" android:layout_width="match_parent">
<TextView android:id="@+id/sell10Text" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content" android:layout_weight="2"></TextView>
<Button android:text="Sell 10" android:enabled="false" android:layout_width="wrap_content" android:id="@+id/sell10Button" android:layout_height="wrap_content" android:layout_weight="1"></Button>
</LinearLayout>
Pertinent part of the dialog creation:
final Dialog alert = new Dialog(this);
alert.setTitle("Sell how many "+(masterRes.get(currentResIndex).getName())+"?");
alert.setContentView(R.layout.selldialog);
TextView tvsellAll = (TextView) alert.findViewById(R.id.sellAllText);
TextView tvsell10 = (TextView) alert.findViewById(R.id.sell10Text);
//etc etc more handles, including buttons
tvsellAll.setText("Sell All ("+String.valueOf(masterRes.get(currentResIndex).getHeld())+") - $"+String.valueOf(calcCost(masterRes.get(currentResIndex).getHeld())));
tvsell10.setText("Sell 10 - $"+String.valueOf(calcCost(10)));
// etc etc more setTexts
btnsell10.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
if (v.isEnabled()) {
int y=masterRes.get(currentResIndex).getHeld();
masterRes.get(currentResIndex).setHeld(y-10);
held -= 10;
money += (calcCost(10));
updateScreen();
alert.tvsellAll.setText("Sell All ("+String.valueOf(masterRes.get(currentResIndex).getHeld())+") - $"+String.valueOf(calcCost(masterRes.get(currentResIndex).getHeld())));
alert.tvsell10.setText("Sell 10 - $"+String.valueOf(calcCost(10)));
alert.tvsellAmt.setText("Sell Amount (0-"+String.valueOf(masterRes.get(currentResIndex).getHeld())+")");
}
}
});
// etc etc other button handlers, alert.show() at the end
Now obviously the setTexts within the button can't resolve, as they can't see the alert I created, they just see OnClickListener.
I tried handling this like I did with my main activity's updater updateScreen(), which is a Runnable
, that is a long list of setTexts and/or invalidates, and is runOnUiThread(updateScreen)
. Works great for the base activity.
I did some copypasta and tried to make a updateSellScreen(), get it to hook into the custom dialog's textviews, but it can't resolve the alert class... I'm kind of lost now.
Is this even possible without trashing everything and just creating a custom view (which I am very averse to trying to tackle this fresh into Android programming...)