ディレクトリに画像が含まれている場合、これは私のコードが完全に実行されますが、ディレクトリに画像 (0) が含まれていない場合、コードは爆発して java.lang.ArrayIndexOutOfBoundsException エラーを表示します。空白またはnullのグリッドビューをロードするにはどうすればよいですか??? これは私のディスプレイのイメージですhttp://tinypic.com/view.php?pic=2zyda9f&s=5 写真をクリックすると(3) hello(0) をクリックすると正常に動作します コードはログ エラーまたはメッセージ java.lang で表示を爆破します.ArrayIndexOutOfBoundsException.
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;
}