0

我尝试在我的代码中使用复选框经过这么多尝试我做到了,但是在我的代码中添加复选框之前我发现了一个问题我的代码进入了一个不包含文件的目录,请参阅此图像

http://tinypic.com/view.php?pic=2zyda9f&s=5

如果任何文件夹包含 (0) 图像我的代码将爆炸 .not 进入并显示空网格如果目录包含至少 (1) 一个图像 mycode 运行完美我该怎么办?我如何处理空目录问题

    GridView gridView;
TextView textView;
    File currentParent;
File[] currentFiles;
String[] currentFilePath;
    boolean CheckArr[]; 
    File root1;
LinearLayout ll;
String filePath;
String PhotosFolder = "Photos Folders";

SimpleAdapter simpleAdapter;

List<ImageData> imList;

Button del;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    del = (Button) findViewById(R.id.canceles);
    del.setOnClickListener(new OnClickListener() {      
        @Override
        public void onClick(View arg0) {

            for(int i=0;i<CheckArr.length;i++)
            {
                if(CheckArr[i] == true)
                {

                        File file = new 
   File(imList.get(i).getImgPath());
                        file.delete();
                }
            }


            inflateListView(currentFiles);
        }
    });


    ll = (LinearLayout) findViewById(R.id.linearlayout1);
    viewLoad = LayoutInflater.from(FileGridActivity.this).inflate(
            R.layout.main, null);
    AddPictures = (ImageButton) findViewById(R.id.add_privacy_contact);

    Addfolder = (Button) findViewById(R.id.Addfolder);

    textView = (TextView) findViewById(R.id.title_name);
    File photos = new File(getFilesDir(), "Photos");
    photos.mkdirs();

    root1 = new File("/data/data/com.myexample/files/");

    currentParent = root1;
    currentFiles = root1.listFiles();


    currentFilePath = new String[currentFiles.length];
    int count = 0;

    for (File f : currentFiles) {
        currentFilePath[count] = f.getAbsolutePath();
        count++;

    }

    gridView = (GridView) findViewById(R.id.grid);
    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            if (currentFiles[position].isDirectory() ) {


                root = new File("/data/data/com.myexample/files/"+ 
             FileName(currentFilePath[position]) + "/");

                currentFiles = root.listFiles();
                inflateListView(currentFiles);
            } else if (currentFiles[position].isFile()) {
                openFile(currentFiles[position]);

                // gridposition = currentFiles[(int) id]);

            }           }
       });


              private void inflateListView(File[] files) {

    imList = new ArrayList<ImageData>();
    ImageData imObj;

    List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

    boolean check;

    for (int i = 0; i < files.length; i++) {
        Map<String, Object> listItem = new HashMap<String, Object>();





        if (files[i].isDirectory()) {





            listItem.put("icon", R.drawable.folder);
            listItem.put("fileName",files[i].getName() + "(" + 
        files[i].list().length + ")");

            listItems.add(listItem);

            simpleAdapter = new SimpleAdapter(this, listItems, 
       R.layout.line,new String[] { "icon", "fileName" }, new int[] { 
           R.id.icon,R.id.file_name });
        //  gridView.invalidateViews();
            gridView.setAdapter(simpleAdapter);

        //  simpleAdapter.notifyDataSetChanged();

            check = false;
        } 
        else 
        {   
            check = true;
            imObj = new ImageData();




            listItem.put("icon", files[i]);

            listItems.add(listItem);

            imObj.setImgPath(files[i].toString());

            imList.add(imObj);

            }


        if(check == true)
        {
            imageAdapter = new          

ImagesAdapter(getApplicationContext(),FileGridActivity.this,0,imList);      
            gridView.setAdapter(imageAdapter);
            imageAdapter.notifyDataSetChanged();
        }
    }



  }

  public class ImagesAdapter extends ArrayAdapter<ImageData> {

    private LayoutInflater mInflater;

    List<ImageData> listItems;

    public ImagesAdapter(Context context,Activity activity, int 
       textViewResourceId, List<ImageData> items) {
            super(context, textViewResourceId, items);
            this.listItems = items;
            mInflater = LayoutInflater.from(context);

            CheckArr = new boolean[items.size()];

            for(int z = 0; z<CheckArr.length;z++)
                CheckArr[z] = false;
    }

    public class ViewHolder{

           public CheckBox checkbox;
           public ImageView imageview;

    }

     public View getView(int position, View convertView, ViewGroup parent) {
            View vi=convertView;
            ViewHolder holder;

                vi = mInflater.inflate(R.layout.galleryitem, null);


                ImageData o = listItems.get(position);

                if(o != null){
                    holder = new ViewHolder();

                    holder.imageview = (ImageView) 
      vi.findViewById(R.id.thumbImage);
                    holder.checkbox = (CheckBox) 
   vi.findViewById(R.id.itemCheckBox);

                    File f = new 
      File(listItems.get(position).getImgPath());
                    holder.imageview.setImageBitmap(decodeFile(f));

                    holder.checkbox.setId(position);
                    holder.imageview.setId(position);

                    holder.checkbox.setOnCheckedChangeListener(new 
        OnCheckedChangeListener() {                 
                        public void 
       onCheckedChanged(CompoundButton arg0, boolean arg1) {
                            // TODO Auto-generated 
     method stub
                            Log.e("id",arg0.getId()+" 
      ");
                            Log.e("bool",arg1+ " ");

                            CheckArr[arg0.getId()] = 
        arg1;

                        }
                    });

                }

            return vi;

        }       
4

0 回答 0