如何实现对不同活动的对话? [英] How to implement a dialog for different activities?

查看:109
本文介绍了如何实现对不同活动的对话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在不同的活动中相同的对话框。我试图让一个 BaseActivitiy 。该活动扩展我的BaseActivity。这工作至今,但现在我想更新,显示对话框时,对话框被关闭的活动。在我的情况更新是指从SQLite数据库填充数据的列表视图。

I want to show the same dialog in different activities. I tried to make a BaseActivitiy. The Activities extends my BaseActivity. That worked so far, but now I want to update the Activity which shows the dialog when the dialog is closed. Update means in my case to fill a listview with data from a SQLite database.

我也试过来检索类名来使用这些活动的更新方法。但它是不可能的更新方法转换到静态的,因为非静态的SQL方法...

I also tried to retrieve the classname to use the update method of those activities. But it is not possible to change the update method to static, because of the non-static SQL methods...

你有什么想法?

活动:

    public class MyActivity extends Dialogs {
        ...
        @Override  
        public boolean onContextItemSelected(MenuItem item) { 
            AdapterContextMenuInfo info = (AdapterContextMenuInfo)  item.getMenuInfo();
            int idx = info.position;
            switch (item.getItemId()) {

                case CONTEXTMENU_ID:
                   showMyDialog(this,DIALOG_ID);
                   break;
            }      
        return true;
       }

       public void update() {
           ...
       }
    }

DialogsClass

DialogsClass

    public class Dialogs extends Activity {
         @Override
         protected Dialog onCreateDialog(int id) {
              ...
         }
         ...
         //Called on Dialog-Butten press
         private void ReloadActivity(){
             if(DialogCalledByClass.contains("MyActivity")) {
                 MyActivity.update();// It doesn't worke because non-static....
              }
              else if(DialogCalledByClass.contains("MyActivity2")) {

              }
         }

         public void showMyDialog(Context ctx,int id) {
             showDialog(id);
             DialogCalledByClass =ctx.getClass().toString();
         }
     }

这就是我曾尝试...

That's what I have tried...

推荐答案

我找到了一个解决方案。谢谢你大卫! Sry基因,我不能投票,因为更少的声誉......

I found a Solution. Thanks to you David!! Sry I couldn't vote up because to less reputation...

private void ReloadActivity(){
    if(DialogCalledByClass.contains("MyActivity")){
        try {
            Method m = DialogActivity.getClass().getMethod("Update");
            try {
                m.invoke(DialogActivity);
            } catch (IllegalArgumentException e) {

                e.printStackTrace();
            } catch (IllegalAccessException e) {

                e.printStackTrace();
            } catch (InvocationTargetException e) {

                e.printStackTrace();
            }


        } catch (SecurityException e) {

            error.d(TAG, "SecurityException"+ e);
            e.printStackTrace();
        } catch (NoSuchMethodException e) {

            Log.d(TAG, "NoSuchMethodException"+ e);
        }

    }
    else if(DialogCalledByClass.contains("MyActivity2")){

    }
}




public void showMyDialog(Activity act,int id){
    showDialog(id);

    DialogCalledByClass = act.getClass().toString();
    DialogActivity = act;

}

这篇关于如何实现对不同活动的对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆