选中复选框时对话框出现问题吗?

时间:2018-07-19 14:26:41

标签: android gridview checkbox dialog android-adapter

我有一个片段,需要从图库中选择一些图像。因此,基本上,我想显示一个对话框,在其中填充缩略图和复选框。 图像和复选框正确显示,但是当我选择一个或多个复选框时,它将在空对象上返回“ JavaNullPointerException”。 如果我使用活动,此代码可以正常工作,但由于存在片段,我不知道错误在哪里。 在我的代码下面:

我调用对话框的方法。

private void scegliFotoDaMostrare(){
    final Dialog d = new Dialog(getActivity());
    d.setContentView(R.layout.activity_show_image);

    ImageList = getSD();

    // gridView1
     gView1 = (GridView)d.findViewById(R.id.gridView1);

    gView1.setAdapter(new ImageAdapter(getActivity(),ImageList));


    // Check All
    Button btnCheckAll = (Button) d.findViewById(R.id.btnCheckAll);
    btnCheckAll.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                checkbox.setChecked(true);
            }
        }
    });

    // Clear All
    Button btnClearAll = (Button) d.findViewById(R.id.btnClearAll);
    btnClearAll.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                checkbox.setChecked(false);
            }
        }
    });

    // Get Item Checked
    Button btnGetItem = (Button) d.findViewById(R.id.btnGetItem);
    btnGetItem.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int count = gView1.getAdapter().getCount();
            for (int i = 0; i < count; i++) {
                LinearLayout itemLayout = (LinearLayout)gView1.getChildAt(i); // Find by under LinearLayout
                CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
                if(checkbox.isChecked())
                {
                    Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());

                    Toast.makeText(getActivity(),checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();

                }
            }
        }
    });
    d.show();

    }

适配器

public class ImageAdapter extends BaseAdapter{        
    private Context context;
    private List <String> lis;

    public ImageAdapter(Context c, List <String> li)
    {
        // TODO Auto-generated method stub
        context = c;
        lis = li;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return lis.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.showimage, null);
        }

        TextView textView = (TextView) convertView.findViewById(R.id.textView1);
        String strPath = lis.get(position).toString();

        // Get File Name
        String fileName = strPath.substring( strPath.lastIndexOf('/')+1, strPath.length() );
        textView.setText(fileName);

        // Image Resource
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        Bitmap bm = BitmapFactory.decodeFile(strPath);
        imageView.setImageBitmap(bm);

        // CheckBox
        CheckBox Chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        Chkbox.setTag(fileName);

        return convertView;

    }
}

获取列表!

    private List <String> getSD() {
     List <String> it = new ArrayList <String>();
     String pathImmagini =  Environment.getExternalStorageDirectory().toString() +
     File.separator + "DCIM"+ File.separator +  File.separator;
     File f = new File(pathImmagini);
     File files[] = f.listFiles();
     String titoloDaCercare = "giretto";      
     for (int i = 0; i <files.length; i++){   
              String fileName = (files[i].getName().substring(0,files[i].getName().indexOf(";"))
                .replace("IMG_",""));
         if (fileName.equals( titoloDaCercare) ){
              File file = files[i];
              Log.d("Count", file.getPath());
              it.add(file.getPath());
         }
     }
    return it;
}

0 个答案:

没有答案