我遇到了这个问题可能是因为我在android框架的基本内部工作中措辞不是很好。
所以我在tablelayout中生成行,这些行有一个onclick动作来打开一个对话框。代码如下所示:
titleText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.update_book);
dialog.setTitle("Update dialog");
// set the custom dialog components - text, image and button
String sIsbn = "-";
if ( book.getIsbn() != null ) {
sIsbn = Long.toString(book.getIsbn());
}
TextView uIsbn = (TextView) dialog.findViewById(R.id.Uisbn);
uIsbn.setText(sIsbn);
Button readBCode = (Button) getActivity().findViewById(R.id.Uread);
readBCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.initiateScan();
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.Uclose);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
在对话框中,我有一个名为readBCode的按钮,当点击它时,条形码扫描开始并在检索到条形码时消失:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
tToast("Cancelled");
} else {
Log.d("MainActivity", "Scanned");
tToast("Scanned: " + result.getContents());
// set the custom dialog components - text, image and button
TextView uIsbn = (TextView) getActivity().findViewById(R.id.Uisbn);
uIsbn.setText(result.getContents());
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
现在我想把这个结果字符串推回到那个活动对话框的Isbn文本视图中,但这当然失败了...我该如何继续完成它呢?
答案 0 :(得分:1)
我在片段中,所以我应该使用IntentIntegrator.forFragment
并且单独解决它。