Android - 在DialogFragment中设置imageView

时间:2014-05-23 08:39:45

标签: android imageview android-dialogfragment

我有一个应用程序,其中我有一个自定义listView,在单击listView中的任何项目时,会显示一个DialogFramgent。现在,我想要做的是将DialogFramgent中的imageView设置为外部存储器中的图像。我知道我可以设置imageView的源:

File imgFile = new  File(“/sdcard/Images/test_image.jpg”);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageViewItem);
    myImage.setImageBitmap(myBitmap);

}

但是,我无法正确初始化我的ImageView变量,因为findViewById给出了错误:`类型ItemDialog的方法findViewById(int)未定义。

以下是我在自定义listView onClickLisenter中调用DialogFragment的部分:

resultListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, final View v, final int position, long id) {

        positionSelected = position;

        SearchResultListViewItem selectedRow = results.get(position);
        String selectedItem = selectedRow.getItemName();
        itemBarcode = selectedRow.getBarcode();

        itemListDB.open();

        ItemDialog itemDialog = new ItemDialog();

        itemDialog.itemName = selectedItem;
        itemDialog.brandName = itemListDB.getBrandFromItemBarcode(itemBarcode);
        itemDialog.UOMName = itemListDB.getUOMFromItemBarcode(itemBarcode);
        itemDialog.priceName = itemListDB.getPriceFromItemBarcode(itemBarcode);
        itemDialog.discountName = itemListDB.getDiscountFromItemBarcode(itemBarcode);
        itemDialog.mListener = SearchResult.this;

        itemDialog.qtyFromList = qtyInput.get(position);
        itemDialog.discFromList = qtyInput.get(position);

        itemListDB.close();
        itemDialog.show(getFragmentManager(), "");

    }
});

这是我的ItemDialog类:

public class ItemDialog extends DialogFragment implements OnClickListener{

    //my variables go over here

    public interface onSubmitListener {
        void setOnSubmitListener(String qty, String disc);  
    }  

    @Override  
    public Dialog onCreateDialog(Bundle savedInstanceState) {  

        final Dialog dialog = new Dialog(getActivity());  
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  

        dialog.setContentView(R.layout.fragment_item_dialog);  
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);

        File imgFile = new  File("/sdcard/Images/test_image.png");
        if(imgFile.exists()){

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

            ImageView myImage = (ImageView) findViewById(R.id.imageViewItem);
            myImage.setImageBitmap(myBitmap);

        }

        dialog.setTitle("Item Details");
        dialog.show();  
        //I set more variables here       
        return dialog;  
    }       

}

那么,有没有人知道如何在DialogFragment中设置imageView的源图像?我被困在这里,坦率地说,我不知道如何继续,如果有人能帮助我,我将非常感激。

谢谢。

4 个答案:

答案 0 :(得分:5)

您需要在inflate

下的Dialog Fragment进行onCreateDialog(...)布局
  LayoutInflater layoutInflater = (LayoutInflater) getActivity()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View layout2 = layoutInflater.inflate(R.layout.contact_filter_popup,null);

  dialog.setView(layout2);

然后使用该特定Imageview

引用您的layout
   ImageView img=(ImageView)layout2.findViewById(R.id.imageViewItem);

<强>更新

@Override  
public Dialog onCreateDialog(Bundle savedInstanceState) {  

    final Dialog dialog = new Dialog(getActivity());  

    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
            WindowManager.LayoutParams.FLAG_FULLSCREEN);  


     LayoutInflater layoutInflater = (LayoutInflater) getActivity()
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout2 = layoutInflater.inflate(R.layout.contact_filter_popup,null);
    dialog.setContentView(layout2);  

    setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);

    ImageView myImage = (ImageView) layout2.findViewById(R.id.imageViewItem);

    File imgFile = new  File("/sdcard/Images/test_image.png");

    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        myImage.setImageBitmap(myBitmap);

    }

    dialog.setTitle("Item Details");
    dialog.show();  

    return dialog;  
}       

答案 1 :(得分:2)

问题是由于生命周期不匹配造成的。在创建对话框时的对话框片段中,它并不意味着创建了视图。由于片段未附加到Activity,因此会导致IllegalArgumentException。为了克服这个问题,请始终在onViewsCreated()/onCreateViews()方法中查看视图后使用/访问您的视图。找到下面的解决方案,工作正常。

public class ImageDetailDialog extends DialogFragment implements OnClickListener{

    private String url ;

    private ImageView imgProdDetail;


    /* (non-Javadoc)
     * @see android.support.v4.app.DialogFragment#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getArguments().getString("url");
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.product_dialog);
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.image_detail_dialog, null);
        imgProdDetail = (ImageView)view.findViewById(R.id.img_prod_detail);
        view.setOnClickListener(this);
        return view;
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

            /** Decode/load YOUR IMAGE HERE **/
        imgProdDetail.setImageResource(R.drawable.image);
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.DialogFragment#onCreateDialog(android.os.Bundle)
     */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //not working
        dialog.setCanceledOnTouchOutside(true);
        return dialog;
    }

    /* (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        if(v!=imgProdDetail)
            this.dismissAllowingStateLoss();
    }

}

答案 2 :(得分:1)

片段没有findViewById方法,你可以在片段的getView()方法上调用它:

  File imgFile = new  File(“/sdcard/Images/test_image.jpg”);
  if(imgFile.exists()){
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ImageView myImage = (ImageView) getView().findViewById(R.id.imageViewItem);
    myImage.setImageBitmap(myBitmap);
  }

我假设你已经夸大了DialogFragment的相应视图。

了解更多信息: http://developer.android.com/reference/android/app/Fragment.html#getView()

答案 3 :(得分:1)

尝试覆盖onCreateView()回调方法,作为片段,可以使用此方法对DialogFragment进行充气,以便稍后可以设置您的布局并查找imageview参考。