2012-04-19 7 views
0

プラグインビューを許可するホームオートメーションアプリケーションを作成しています。私は別のプロジェクト(APK)のサンプルプラグインとしてクラスを作成することができました:動的にロードされるクラスから.showDialog()を呼び出す方法

public class MyTestClass_IRDroidUIPlugIn extends Button implements IRDroidInterface{ 
    Context mContext; 
    public MyTestClass_IRDroidUIPlugIn(Context context) { 
     super(context); 
     mContext = context; 
     setText("I was loaded dynamically! (1)"); 
     setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
        // how do I show the dialog from here? 
       Activity.showDialog(1); 
      }} 
       ); 
    } 
    public Dialog buildConfigDialog(int ID){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
     builder.setMessage("Click the Button...(1)") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.dismiss(); 
       }   
      }); 
     return builder.create(); 
    } 

} 

私は、実行時にこのクラスをロードし、そのインスタンスを作成することができます。

 try { 
     final File filesDir = this.getFilesDir(); 
     final File tmpDir = getDir("dex", 0); 
     final DexClassLoader classloader = new DexClassLoader(filesDir.getAbsolutePath()+"/testloadclass.apk", 
       tmpDir.getAbsolutePath(), 
       null, this.getClass().getClassLoader()); 
     final Class<View> classToLoad = 
       (Class<View>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn"); 
     mybutton = (View) classToLoad.getDeclaredConstructor(Context.class).newInstance(this); 
     mybutton.setId(2); 
     main.addView((View)mybutton); 
     } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    setContentView(main); 
} 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case 1: 
      return ((IRDroidInterface) mybutton).buildConfigDialog(id); 
    } 
    return null; 
} 

私はプラグインが設定ダイアログを表示できるようにします。 .showDialog(ID)を使用できるように、このクラスにActivityオブジェクトを渡す方法はありますか。ダイアログライフサイクルを適切に管理できるようにするには、これが理想的です。

ありがとうございます。

答えて

2

多分私は何かを逃していますが、なぜあなたはこれのようなことをすることができませんか?

public class MyTestClass_IRDroidUIPlugIn extends Button implements IRDroidInterface{ 
    Activity mContext; 
    public MyTestClass_IRDroidUIPlugIn(Activity context) { 
     super(context); 
     mContext = context; 
     ... 
     setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       mContext.showDialog(1); 
      }} 

     ); 

..... 
} 

このクラスを作成するときは、すでにActivityをこのクラスに渡しているので、そのまま使用してください。

+0

私はこのクラスに 'Context'を渡しています。アクティビティはコンテキストですが、残念ながらコンテキストはアクティビティではありません。 'エラー: 方法にShowDialog(int)がContext型\t MyTestClass_IRDroidUIPlugIn.java \t/testloadclass/SRC/COM/strutton /アンドロイド/ testloadclass \tライン22 \tのJava Problem' – cstrutton

+0

@cstruttonについて定義されていません:あなたは私の答えに気付いた場合、私はコンストラクタとメンバ変数のパラメータ型をContextからActivityに変更しました。 Contextをコンストラクターに渡すときは、実際にアクティビティーを渡しています。単にそれを宣言して使用するだけです。もちろん抽象スーパークラスも変更する必要があります。 –

+0

もう一度ありがとうございます。同じコードに別の問題があるようです。私はインターフェイスを2回ロードしていた(メインコードに1回、プラグインに1回)。一度私はそれを考え出し、残りは働き始めた。しかし、私はContextにアクティビティをキャストするために上記のコードを編集しました。 Eclipseは不平を言っていました。私はテストクラスがButtonを拡張するので、これを行いました。これは受け入れられますか? – cstrutton

関連する問題