我有一个应用程序,其中我有一个自定义 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 的源图像?我被困在这里,坦率地说,我不知道如何继续,如果有人能帮助我,我将不胜感激。

谢谢。

请您参考如下方法:

您需要在 onCreateDialog(...)

下的 Dialog Fragmentinflate 布局
  LayoutInflater layoutInflater = (LayoutInflater) getActivity() 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
  View layout2 = layoutInflater.inflate(R.layout.contact_filter_popup,null); 
 
  dialog.setView(layout2); 

然后使用特定的 layout

引用您的 Imageview
   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;   
}        


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!