Updating a textview inside a custom dialog via a b

2020-06-30 05:44发布

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...)

2条回答
Deceive 欺骗
2楼-- · 2020-06-30 06:16

Declare your TextViews as final. You'll still be able to set their texts, it just means you won't be able to reassign the variable references. Don't do alert.tv as the TextView is not an instance variable of your dialog, but rather of the method with which you are creating your dialog. This is the easy way. You could also declare your TextViews as instance variables of your Activity and then update them through a handler.

alert.setTitle("Sell how many "+(masterRes.get(currentResIndex).getName())+"?");
alert.setContentView(R.layout.selldialog);
final TextView tvsellAll = (TextView) alert.findViewById(R.id.sellAllText);
final 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();
               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)));
               tvsellAmt.setText("Sell Amount (0-"+String.valueOf(masterRes.get(currentResIndex).getHeld())+")");


           }
        }
    });
查看更多
叛逆
3楼-- · 2020-06-30 06:19

In activity where you creates your dialog, you can declare private variables of dialog, textviews, etc, then they will be accessible anywhere in activity.

        dialogA = new Dialog(myActivity.this, android.R.style.Theme_Dialog);
    dialogA.setContentView(R.layout.myDialog);
    // ...      
    tv1 = (TextView) dialogA.findViewById(R.id.textView1);

    Button b1 = (Button) dialogA.findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String s1 = tv1.getText().toString();
            Toast.makeText(myActivity.this, s1, Toast.LENGTH_SHORT).show();
            dialogA.cancel();
        }
    });

    dialogA.show();
查看更多
登录 后发表回答