私はAndroid開発に不慣れで、ChildBrowserプラグインにあるものと同様のカスタムダイアログプラグインを実装する必要がありました。
特定のダイアログアクションでのJSコールバックイベントなど、このダイアログのほとんどの機能を実装できましたが、表示されている間、JSからダイアログにデータを送信することはできませんでした。
シナリオは次のとおりです。ダイアログのスピナーが変更されると、JSコールバックが呼び出され、JSコードが何らかの処理(sqlStorageデータへのアクセスなど)を実行し、その後、ダイアログのいくつかのビューを更新する必要があります。私の現在のコード(関連性のないものなし):
CustomDialogPlugin.java:
public class CustomDialogPlugin extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("show")) {
result = this.showDialog(args.optJSONObject(0), callbackId);
// ...
} else if (action.equals("update")) {
this.updateData(args.optJSONObject(0));
}
} catch(JSONException e) {
// ...
}
}
// Show the dialog
public String showDialog(JSONObject options, final String callbackId) {
if (options != null) {
// Handle options
}
// Create the child dialog in new thread
Runnable runnable = new Runnable() {
public void run() {
mDialog = new Dialog(ctx);
// Dialog layouts, views and listeners setup
// ...
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
// ...
// Prepare JSON data to send and call a callback
sendUpdate(CHANGED_EVENT, obj, true);
}
@Override
public void onNothingSelected(AdapterView adapter) {
// ...
}
});
// ...
mDialog.show();
}
};
this.ctx.runOnUiThread(runnable);
return "";
}
// Create a new plugin result and send it back to JavaScript
private void sendUpdate(int eventType, JSONObject obj, boolean keepCallback) {
if (this.savedCallbackId != null) {
// ...
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
Result.setKeepCallback(keepCallback);
this.success(result, this.savedCallbackId);
}
}
// Parse data received from JS and upate dialog
protected String updateData(JSONObject data) {
// Here I need to update some views of mDialog
// Can't access them from here
}
}
customdialog.js:
var CustomDialog = function() {};
CustomDialog.CHANGED_EVENT = 1;
CustomDialog.prototype.show = function(data) {
return PhoneGap.exec(this._onEvent, this._onError, 'CustomDialogPlugin', 'show', [data]);
};
CustomDialog.prototype.update = function(data) {
return PhoneGap.exec(this._onEvent, this._onError, 'CustomDialogPlugin', 'update', [data]);
};
CustomDialog.prototype._onEvent = function(data) {
if (data.type == CustomDialog.CHANGED_EVENT && typeof window.plugins.CustomDialog.onChange === "function") {
window.plugins.CustomDialog.onChange(data);
}
// ...
};
CustomDialog.prototype._onError = function(data) {
// ...
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('customDialog', new CustomDialog());
});
test.html:
// ...
window.plugins.customDialog.onChange = function(data) {
// ...
window.plugins.customDialog.update(some_other_data);
}
Handler
内部を作成し、Runnable
から送信されたメッセージを処理するためにそれを呼び出しようとしましたupdateData
が、どうやら私は何か間違ったことをしています。
多分私は物事を複雑にしすぎていて、JSコールバックからデータ更新を達成するためのより簡単な方法がありますか?
前もって感謝します。