如何实现不同活动的对话框?

发布于 2024-12-06 01:05:08 字数 1517 浏览 1 评论 0原文

我想在不同的活动中显示相同的对话框。我尝试制作一个BaseActivitiy。这些活动扩展了我的基本活动。到目前为止,这是有效的,但现在我想更新当对话框关闭时显示对话框的活动。在我的例子中,更新意味着用 SQLite 数据库中的数据填充列表视图。

我还尝试检索类名以使用这些活动的更新方法。但是不可能将更新方法更改为静态,因为非静态 SQL 方法...

您有什么想法吗?

活动:

    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

    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();
         }
     }

这就是我尝试过的......

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.

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

Do you have any idea?

Activity:

    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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最后的乘客 2024-12-13 01:05:08

例如...您可以创建自己的对话框,而不是创建 BaseActivity:

class myDialog extends AlertDialog {
   Activity myActivity;

   public myDialog(Activity myAct){
      myActivity=myAct;
   }

   @Override
   public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.my_dialog);
      ...
      ...
   }

   @Override
   public void dismiss(){
      myActivity.update();
   }


   @Override
   public void cancel(){
      myActivity.update();
   }

}

我不知道我是否理解了您的问题,但这是一个想法。我希望它对你有帮助。

For example... Instead of create a BaseActivity you could create your own Dialog:

class myDialog extends AlertDialog {
   Activity myActivity;

   public myDialog(Activity myAct){
      myActivity=myAct;
   }

   @Override
   public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.my_dialog);
      ...
      ...
   }

   @Override
   public void dismiss(){
      myActivity.update();
   }


   @Override
   public void cancel(){
      myActivity.update();
   }

}

I don't know if I've understood your question, but it's an idea. I hope it help you.

囚你心 2024-12-13 01:05:08

我找到了一个解决方案。谢谢你大卫!!抱歉,我无法投票,因为声誉较低......

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;

}

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文