How to send a value from a CustomDialog to an Activity?

0

In the activity_main.xml - > I have a button, which when pressed - > open a custom list view, where when you press the element 1 - > open a custom alert dialog, - > which has 2 buttons and when pressed the Button A - > it takes to change the text of the EditText that is in the activity_main.xml using .setText. Until now I have not been able to achieve this, using different options, the main error I have is the NullpointerExeption error because it does not find the TextView referenced, but I want to know what other options I have to achieve this. I appreciate any suggestions!

ViewDialog.java

final TextView KMLabel = (TextView)activity.findViewById(R.id.KMlabel);



Button KMPerH = (Button)dialog.findViewById(R.id.KmPerH);
    KMPerH.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Intent intent = new Intent(mActivity, MillesPerHour.class);
            //mActivity.startActivity(intent);

            KMLabel.setText("MLL/H");

        }
    });
    
asked by Daniel Prado 02.04.2018 в 00:09
source

1 answer

0

At first glance, your problem seems to be how are you defining your variable activity . If you are using your variable activity to cast the context of Dialog with getContext() to Activity , it will cause a casting error since the Dialog is handled with ContextThemeWrapper and can cause you as a result:

  

java.lang.ClassCastException: android.view.ContextThemeWrapper can not   be cast to android.app.Activity

Now, if you could pass your context in a constructor of your ViewDialog and that context you casted it to your MainActivity or what is called the Activity linked to your Layout activity_main.xml :

. . .
    private MainActivity activity;

    public ViewDialog(Context context){
        this.activity = (MainActivity)context; // Solo podras hacer esto si tu ViewDialog lo crea la actividad MainActivity, de lo contrario devolvera null
    }
. . .

And even if you receive the null , you must make sure that the id is the one that belongs to your root that loads this activity, since that way it should work. But still, the problem is the following

The direct interaction between the Activity and a Context that is very far from it is usually a tedious process using onActivityResult , so many choose to create a class singleton for communication with an Activity. It works quite well, especially when you try to get a callback from an Activity somewhere in my application that can not capture that event in a "natural" way, such as onBackPressed .

If you are using many references of your Activity out of it. The moment you change that reference, you should make changes on all sides where you used those references, so your code would not be very maintainable or would not support "small" changes.

Use an interface to send the value

The recommended thing here is to use an interface that communicates your action from your Dialog to the Activity. If the activity handles too many interfaces you can handle the interaction interfaces in a clase to part, and your Activity that implements the interfaces that your clase creates or capture the data in onNewIntent by Bundles (for this you need to define certain flags before starting your activity again.)

An example of how to create an interface and implement it:

In your ViewDialog you do this: Define an interface.

public interface OnTextViewDataSet{
      void onDataSet(String text);
}

A private variable:

private OnTextViewDataSet listener = null;

And in your constructor:

. . .

public ViewDialog(Context context){
     this.activity = (MainActivity)context; // Solo podras hacer esto si tu ViewDialog lo crea la actividad MainActivity, de lo contrario devolvera null
     if(activity instanceof OnTextViewDataSet)
        this.listener = (OnTextViewDataSet)activity;
}

. . .

In your onClick you invoke the interface:

@Override
public void onClick(View view) {
    String text = "MLL/H";
    if(listener != null)
      listener.onDataSet(text);

}

And in your MainActivity or whatever the Activity is where you want to change the text, you implement the interface, example:

public MainActivity extends Activity implements ViewDialog.OnTextViewDataSet

And you overwrite the method of your interface:

@Override
public void onDataSet(String text) {
    KMLabel.setText(text);
}

Where KMLabel is your global reference of TextView and should not be null. So you can be sure that if you ever remove or change the KMLabel reference, your Activity is responsible for making the necessary changes and you will not have to change the reference in all the classes where you used it with a forced "findViewById".

You can read more in the official documentation of the interaction with the Activitys .

    
answered by 19.05.2018 в 01:13