我有一个活动和自定义对话框。该活动有两个textview和一个图像视图。该对话框具有textview和imageview。当用户单击列表视图中的项时,对话框显示。我试图从活动中获取imageview以显示在对话框的imageview中。在一些帮助下,我从活动到对话框获得了textview。但我也没有传递图像的问题。
我的自定义对话框:
public class MyDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
//builder.setView(inflater.inflate(R.layout.dialogb, null));
View content = inflater.inflate(R.layout.dialogb, null);
TextView dialogt = (TextView) content.findViewById(R.id.dialog_text);
ImageView dialogImg = (ImageView)content.findViewById(R.id.dialog_pic);
dialogImg.setId(getArguments().getInt("id"));
dialogt.setText(getArguments().getCharSequence("text"));
builder.setView(content);
builder.setMessage(R.string.dialog_Event)
.setPositiveButton(R.string.add_to_cal, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
Toast.makeText(MyDialogFragment.this.getActivity(), "testing", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
在我的活动中我使用这种方法:
public void confirmEvent(CharSequence text, int id) {
DialogFragment mDialog = new MyDialogFragment();
Bundle args = new Bundle();
args.putCharSequence("text", text);
args.getInt("id", id);
mDialog.setArguments(args);
mDialog.show(getSupportFragmentManager(), "missiles");
}
这是我的onItemclick:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//String item = ((TextView)view).getText().toString();
int img = ((ImageView)view.findViewById(R.id.event_pic)).getId();
String txt =((TextView)view.findViewById(R.id.subTitle_single)).getText().toString();
//Toast.makeText(Homepage.this,img , Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
confirmEvent(txt, img);
}
我真的不想使用Intent,但如果这是唯一的方法,请告诉我实施的最佳方法。
///// EDITED ///
更改为dialogImage.setImageResource(如评论中所示)
View content = inflater.inflate(R.layout.dialogb, null);
TextView dialogt = (TextView) content.findViewById(R.id.dialog_text);
ImageView dialogImg = (ImageView)content.findViewById(R.id.dialog_pic);
//dialogImg.setImageResource(getArguments().getImageResource(R.id.event_pic));
dialogImg.setImageResource(getArguments().getInt("id"));
//ImageView dialogImg2 = (ImageView)dialogt
dialogt.setText(getArguments().getCharSequence("text"));
builder.setView(content);
builder.setMessage(R.string.dialog_Event)
我的onitemclick:
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//String item = ((TextView)view).getText().toString();
int img = ((ImageView)view.getTag(R.id.event_pic)).getId();
String txt =((TextView)view.findViewById(R.id.subTitle_single)).getText().toString();
//Toast.makeText(Homepage.this,img , Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
confirmEvent(txt, img);
当我点击一个项目时,它现在崩溃了。如果我错过了图像位置,我该如何将它放入代码?
答案 0 :(得分:0)
通过setTag()方法将图像位置设置为列表项,并在onItemClick方法中使用view.getTag()方法获取位置
已编辑:在MyDialogFragment类中,您必须设置图像资源。您只设置了无效的图片ID。 使用diaolgImage.setImageResource('image of image / drawable of the image')
答案 1 :(得分:0)
如果您可以获取图像位置,则在onItemClick()方法中将图像路径保存在SharedPreferences中。
SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
shared.edit().putString("image_path", "path").commit();
在对话框窗口中,您可以检索它,
SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
String path=shared.getString("image_path", null);
否则您可以将其保存在缓存中,但要注意当设备内部存储空间不足时,Android可能会删除这些缓存文件以恢复空间。