how to show Progress Dialog in Non-Activity class inside list view

给你一囗甜甜゛ 提交于 2020-01-25 11:51:46

问题


I have adapter class for Listview that is inside a class which is name AdapterItems that extended in ArrayAdapter.

know how can I use progress dialog in this class that isn't extended in Activity?

this is my fill method for listview

public void fill(final ArrayAdapter<RssParser.Item> adapter, final RssParser.Item item, final int position)
    {

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {

                ActivityStartup.handler.post(new Runnable() {

                    @Override
                    public void run() {
                        ActivityStartup.dismissLoadingDialog();
                        txtTitle.setText(item.title);
                        txtDate.setText(item.pubDate);
                        txt_time.setText(item.time);
                        txtDescription.setText(item.description);
                        layoutRoot.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                if (progress == null) {
                                    progress = new ProgressDialog(v.getContext());
                                    progress.setTitle("title");
                                    progress.setMessage("loading");
                                }
                                progress.show();
                                G.SelectedApplication = item;

                                Intent intent = new Intent(G.context, Adapter_Description.class)
                                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.putExtra("INPUT", item.link + "");
                                intent.putExtra("INPUT_DESC", item.description + "");
                                G.context.startActivity(intent);
                            }
                        });

                    }
                });
            }
        });
        thread.start();

    }
}

when I clicked on the items on the list view to see the dialog I got a crash and this is my Log

09-06 12:04:10.368: E/AndroidRuntime(5112): FATAL EXCEPTION: main 09-06 12:04:10.368: E/AndroidRuntime(5112): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.view.ViewRootImpl.setView(ViewRootImpl.java:571) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.app.Dialog.show(Dialog.java:281) 09-06 12:04:10.368: E/AndroidRuntime(5112): at com.myApp.course.app.feed_reader.AdapterItems$ViewHolder$1$1$1.onClick(AdapterItems.java:71) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.view.View.performClick(View.java:4204) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.view.View$PerformClick.run(View.java:17355) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.os.Handler.handleCallback(Handler.java:725) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.os.Handler.dispatchMessage(Handler.java:92) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.os.Looper.loop(Looper.java:137) 09-06 12:04:10.368: E/AndroidRuntime(5112): at android.app.ActivityThread.main(ActivityThread.java:5041) 09-06 12:04:10.368: E/AndroidRuntime(5112): at java.lang.reflect.Method.invokeNative(Native Method) 09-06 12:04:10.368: E/AndroidRuntime(5112): at java.lang.reflect.Method.invoke(Method.java:511) 09-06 12:04:10.368: E/AndroidRuntime(5112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 09-06 12:04:10.368: E/AndroidRuntime(5112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 09-06 12:04:10.368: E/AndroidRuntime(5112): at dalvik.system.NativeStart.main(Native Method) 09-06 12:04:36.292: E/InputDispatcher(467): channel 'a69034a0 com.myApp.course.app.feed_reader/com.myApp.course.app.feed_reader.ActivityStartup (server)' ~ Channel is unrecoverably broken and will be disposed!


回答1:


Just pass the Activity Context to the Constructor of your non_activity class , then use that context to show your ProgressDialog

private Activity activityContext;

Public YourClassName(Activity activityContext)
{
  this.activityContext = activityContext;
}

then use this Context to initialize a ProgressDialog :

progressDialog = new ProgressDialog(activityContext);



回答2:


You have to pass

Activity Context

to your non activity class. And then use that context to display Dialog



来源:https://stackoverflow.com/questions/25698117/how-to-show-progress-dialog-in-non-activity-class-inside-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!