问题
I have an activity and custom dialog. the activity has two textview and 1 image view. the dialog box has a textview and an imageview. when a user clicks the item in the listview the dialog box shows. i am trying to get the imageview from the activity to show in the dialog box's imageview. With some help, i got the textview from the activity to the dialog box. but i am having problems passing the image as well.
My custom dialog:
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();
}
}
in my activity i use this method:
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");
}
this is my 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);
}
I really i didnt want to use Intents for this, but if that is the only way please let me know the best way to implement.
/////EDITED///
after changing to dialogImage.setImageResource ( as suggested in comments)
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)
my 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);
it now crashes when i click an item. if i am missing the image position, how do i put it into the code?
回答1:
Set the image location to the list item by setTag() method and in onItemClick method use view.getTag() method to get the location
Edited : In MyDialogFragment class you have to set the image resource. You have set only image id which won't work. Use diaolgImage.setImageResource('id of image /drawable of the image ')
回答2:
If you can get the image location, within onItemClick() method save the image path in SharedPreferences.
SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
shared.edit().putString("image_path", "path").commit();
Inside your dialog window you can retrieve it,
SharedPreferences shared=getSharedPreferences("app_name", Activity.MODE_PRIVATE);
String path=shared.getString("image_path", null);
Or else you can save it in cache,But beware when the device is low on internal storage space, Android may delete these cache files to recover space.
来源:https://stackoverflow.com/questions/22084529/how-to-pass-image-from-listview-item-to-a-dialog-box-without-the-use-of-an-inten