如何通过从列表视图项图像对话框而不使用意图 [英] how to pass image from listview item to a dialog box WITHOUT the use of an intent

查看:129
本文介绍了如何通过从列表视图项图像对话框而不使用意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动和自定义对话框。该活动有两个TextView的1张图像显示。该对话框中有一个TextView和ImageView的。当用户单击ListView中的项目对话框中显示。我试图从活动获得的ImageView在对话框中的ImageView的显示。随着一些帮助,我从活动到对话框中的TextView。但我有过的形象,以及问题。

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.

我的自定义对话框:

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");
    }

这是我的onItemclick:

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 ///

更改为dialogImage.setImageResource后(所建议的评论)

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)

我onitemclick:

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);

当我点击一个项目,现在崩溃。如果我错过了图像的位置,我怎么把它放到code?

it now crashes when i click an item. if i am missing the image position, how do i put it into the code?

推荐答案

如果你能得到的图像的位置,可onItemClick()方法保存的共享preferences 的。

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);

否则你可以将其保存在缓存,但在设备处于低内部存储空间提防,Android的可以删除这些缓存文件来​​恢复空间。

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.

这篇关于如何通过从列表视图项图像对话框而不使用意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆