懒得打字了,直接把这个类的代码贴上来
public class MainActivity extends Activity { private TextView tv_shuju; private ListView listView; private ArrayAdapter<String> adapter; static String projectfilepath = "//sdcard//麻城市宅基地资料//"; private String[] listParents; private PopupWindow popupWindow; private ArrayList<String> folder; private String path; public String getPath() { return path; } public void setPath(String path) { this.path = path; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 判断sd卡是否存在 boolean sdCardExist = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); // 获得sd卡根目录 if (sdCardExist) { File dir = Environment.getExternalStorageDirectory(); path = dir.getAbsolutePath() + "/麻城市宅基地资料/"; // 文件夹List集合 folder = getFolder(path); // 适配器数据 listParents = listParent(folder); } else { Toast.makeText(getApplicationContext(), "没有SD卡", 0).show(); } // 获取控件ID tv_shuju = (TextView) findViewById(R.id.tv_shuju); tv_shuju.setText("点击下面修改数据"); listView = (ListView) findViewById(R.id.listView1); if (listParents.length == 0) { tv_shuju.setText("没有数据"); } // listview适配器 if (adapter == null) { adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listParents); listView.setAdapter(adapter); } else { adapter.notifyDataSetChanged(); } // listview 点击监听 listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { System.out.println(position); if (popupWindow != null) { popupWindow.dismiss(); popupWindow = null; } int[] location = new int[2]; view.getLocationInWindow(location); // 获取到了x 和y的坐标 存放到了 2个元素的数组中 int x = location[0]; int y = location[1]; // 弹出popupWindow View popup_view = View.inflate(getApplicationContext(), R.layout.popup_soft_manager, null); Button btn_del = (Button) popup_view.findViewById(R.id.btn_del); // 删除按钮 btn_del.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 获取到点击的文件夹名称 String name = listParents[position]; // 删除文件夹 delFolder(name); popupWindow.dismiss(); popupWindow = null; shuaxin(); } }); // 更新/重命名按钮 Button btn_update = (Button) popup_view.findViewById(R.id.btn_update); btn_update.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), UpdateActivty.class); intent.putExtra("floerName", folder.get(position)); intent.putExtra("path", path); startActivityForResult(intent, 1); popupWindow.dismiss(); popupWindow = null; } }); // 取消按钮 Button btn_canel = (Button) popup_view.findViewById(R.id.btn_canel); btn_canel.setOnClickListener(new OnClickListener() { public void onClick(View v) { popupWindow.dismiss(); popupWindow = null; } }); popupWindow = new PopupWindow(popup_view, -2, -2); // 给popupWindow设置背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 展示popupWindow 参数 1 挂载的父窗体 popupWindow.showAtLocation(parent, Gravity.LEFT | Gravity.TOP, x + 50, y + 60); } }); // listview 滑动监听 listView.setOnScrollListener(new OnScrollListener() { public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (popupWindow != null) { popupWindow.dismiss(); popupWindow = null; } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode==2){ shuaxin(); } } // 刷新适配器 private void shuaxin() { folder.clear(); listParents = null; folder = getFolder(path); // 适配器数据 listParents = listParent(folder); adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listParents); listView.setAdapter(adapter); } //重命名文件 public void renameFloder(String name,String reName){ File file = new File(path + name); file.renameTo(new File(reName)); } // List转换为String[] public String[] listParent(ArrayList<String> list) { int size = list.size(); String[] array = new String[size]; for (int i = 0; i < list.size(); i++) { array[i] = (String) list.get(i); } return array; } // 删除文件夹 public void delFolder(String name) { File file = new File(path + name); if (file.isFile()) { file.delete(); return; } if (file.isDirectory()) { File[] chaFiles = file.listFiles(); if (chaFiles == null || chaFiles.length == 0) { file.delete(); return; } for (int i = 0; i < chaFiles.length; i++) { chaFiles[i].delete(); } file.delete(); } } // 检查文件夹是否存在 boolean isFolderExists(String strFolder) { File file = new File(strFolder); return file.exists(); } // 扫描文件夹目录下的文件夹名字 public ArrayList<String> getFolder(String path) { ArrayList<String> names = new ArrayList<String>(); File file = new File(path); if (file.isDirectory()) { File[] array = file.listFiles(); for (int i = 0; i < array.length; i++) { File f = array[i]; names.add(f.getName()); System.out.println("文件夹名称" + f.getName()); } } return names; } // 扫描文件夹里面的数据 public ArrayList<String> folderScan(String path) { File file = new File(path); ArrayList<String> names = new ArrayList<String>(); if (file.isDirectory()) { File[] array = file.listFiles(); for (int i = 0; i < array.length; i++) { File f = array[i]; if (f.isFile()) {// FILE TYPE String name = f.getName(); if (name.contains(".jpg")) { names.add(name); fileScan(f.getAbsolutePath()); } } else {// FOLDER TYPE folderScan(f.getAbsolutePath()); } } } return names; } // 扫描 public void fileScan(String file) { Uri data = Uri.parse("file://" + file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); } protected void onDestroy() { // 避免窗体泄露 if (popupWindow != null) { popupWindow.dismiss(); popupWindow = null; } super.onDestroy(); } }
时间: 2024-10-08 06:36:51