开发者

How to implement a dialog for different activities?

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
         pro开发者_运维知识库tected 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...


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.


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;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜