smithonaction
smithonaction

Reputation: 111

update textview in activity from Fragment

How to update TextView in Activity A from DialogFragment ?

in my case, after click the close button in DialogFragment, i need to call the method that can change TextView

this is method from Activity:

public void setCountText(String text){
    CountItem = (TextView) findViewById(R.id.tv_numOfItem);
    CountItem.setText(text);
}

Onclick Button in DialogFragment:

View.OnClickListener closeHandler = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        new menufeedActivity().setQountText(getCount);


        InfoOrderDialog.this.dismiss();

    };

};

Anyone know how to solve this?

04-29 03:08:17.513: E/AndroidRuntime(1738): FATAL EXCEPTION: main
04-29 03:08:17.513: E/AndroidRuntime(1738): java.lang.NullPointerException
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.app.Activity.findViewById(Activity.java:1744)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.example.android.rssfeed.menufeedActivity.setQountText(menufeedActivity.java:169)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.example.android.rssfeed.InfoOrderDialog$2.onClick(InfoOrderDialog.java:110)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.view.View.performClick(View.java:3110)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.view.View$PerformClick.run(View.java:11928)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.os.Handler.handleCallback(Handler.java:587)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.os.Looper.loop(Looper.java:132)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.app.ActivityThread.main(ActivityThread.java:4025)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at java.lang.reflect.Method.invoke(Method.java:491)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 10

Views: 16311

Answers (4)

P. Mohanta
P. Mohanta

Reputation: 140

In kotlin

val textview = activity!!.findViewById<View>(R.id.textView) as TextView
        textview.text = "your_text"

Upvotes: 3

WitaloBenicio
WitaloBenicio

Reputation: 3682

Just do this.

TextView textview = (TextView)getActivity().findViewById(R.id.yourtextview);
textview.setText("text");

Upvotes: 12

Vishal Vijay
Vishal Vijay

Reputation: 2528

You can call it like this:

Replace:

 new menufeedActivity().setQountText(getCount);

With:

((menufeedActivity) getActivity()).setCountText(getCount);

Upvotes: 18

DuKes0mE
DuKes0mE

Reputation: 1131

In your Fragment: call getActivity().startActivityForResult - Method before you finish and catch it in your activity which hosts the fragment with:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

//your method to update textview
setCountText();

}

Edit: I found a better solution for you. Check this: https://stackoverflow.com/a/15121529/2245646

Upvotes: 0

Related Questions