android中的getwindow()不会清除先前的图像

时间:2018-08-30 14:58:53

标签: android listview

我正在使用requestWindowFeature来显示具有可点击列表视图的自定义对话框。我想根据项目选择更改对话图像,所以我要制作一个数组。

一切正常,但是当我单击ListView中的第二项时,也会弹出一个窗口,向我显示上一张图像,请帮忙:

此处是JAVA代码:

int[] images=new int[5];                                                                                                                                                                                                                             
String[] mondaylist={"Incline Betch Press","Flat Dumbbell","Cable Flies","Incline Cable Flies","Seated Pec Fly"};                       
String[] tue_list={"Lat Pull Downs","Lat Close grip","Seated Rows","T-Bar Rows","Cable pull Downs"};                                    
String[] wed_list={"Leg extension","Lying Legs curls","Seated Leg curls","Calf raise","iui"};                                           
String[] thu_list={"Military Press","Seated Dumbbell press","Front Dumbbell Press","Dumbbell Fly","Dumbbell Shrungs"};                  
String[] fri_list={"Dumbbell Curls","Preacher Curls","Cable Biceps Curls","Rope Push downs","Behind the Neck","Triceps Extension"};  

@Override                                                                                                                               
protected void onCreate(Bundle savedInstanceState) {                                                                                    
    final Dialog builder = new Dialog(this);                                                                                            
    builder.requestWindowFeature(Window.FEATURE_SWIPE_TO_DISMISS);                                                                      
    super.onCreate(savedInstanceState);                                                                                                 
    setContentView(R.layout.activity_worklist);                                                                                                                                                                     
    ListView l1=(ListView) findViewById(R.id.listview);                                                                                 
    CustomAdapter ca=new CustomAdapter();                                                                                                                                                                                              
    l1.setAdapter(ca);                                                                                                                  
    l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {                                                                   

        @Override                                                                                                                       
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                                                 
            builder.getWindow().setBackgroundDrawable(                                                                                  
                    new ColorDrawable(android.graphics.Color.TRANSPARENT));                                                             
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {                                                      
                @Override                                                                                                               
                public void onDismiss(DialogInterface dialogInterface) {                                                                

                }                                                                                                                                                                                                                                       
            });                                                                                                                         

            ImageView image = new ImageView(worklist.this);                                                                                                                                                           
            image.setImageResource(images[i]);                                                                                                                                                                     
            builder.addContentView(image, new LinearLayout.LayoutParams(                                                                
                    ViewGroup.LayoutParams.MATCH_PARENT,                                                                                
                    ViewGroup.LayoutParams.MATCH_PARENT                                                                                 

            ));                                                                                                                                                                                                                      
            builder.show();                                                                                                                                                                                             
        }                                                                                                                               
    });

1 个答案:

答案 0 :(得分:0)

这里的错误是您试图重用现有对话框。如果您阅读Dialog#addContentView的文档,则会发现以下内容:

  

在屏幕上添加其他内容视图。在任何之后添加   屏幕中的现有视图-不会删除现有视图。

因此您将图像添加到另一图像的上方,并在第二次调用中将它们都显示出来。相反,您应该在Dialog click方法内创建onItemClick实例。

    @Override                                                                                                                       
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
        Dialog builder = new Dialog(MyActivity.this);                                                                                            
        builder.requestWindowFeature(Window.FEATURE_SWIPE_TO_DISMISS);                                                 
        builder.getWindow().setBackgroundDrawable(                                                                                  
                new ColorDrawable(android.graphics.Color.TRANSPARENT));                                                             
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {                                                      
            @Override                                                                                                               
            public void onDismiss(DialogInterface dialogInterface) {                                                                

            }                                                                                                                                                                                                                                       
        });
        ...