当前位置: 代码迷 >> 综合 >> ListView 获取数据和删除一行后更新
  详细解决方案

ListView 获取数据和删除一行后更新

热度:59   发布时间:2024-01-21 17:25:44.0

private  void getFileList(String path) {// 设置标题// 将当前文件夹路径设置为默认文件夹路径currentFolderPath = path;// 获取sd卡下所有文件的文件名String[] fileNames = fileUtil.getFileNames(currentFolderPath);fileList = new ArrayList<HashMap<String, Object>>();// 在所有文件的前面添加一个..文件夹,用于浏览上一个文件夹HashMap<String, Object> lastFolder = new HashMap<String, Object>();lastFolder.put("FileImage", R.drawable.file_folder);lastFolder.put("FileName", "返回上一级目录");fileList.add(lastFolder);// 1.先判断是否是文件夹(根据文件路径判断)isDirectory()// 2.判断是否是图片文件(根据文件后缀判断)isImageFile()for (String fileName : fileNames) {HashMap<String, Object> hashMap = new HashMap<String, Object>();File file = new File(currentFolderPath + "/" + fileName);// 是文件夹if (file.isDirectory()) {hashMap.put("FileName", fileName);hashMap.put("FileImage", R.drawable.file_folder);}// 不是文件夹else {// 是文本/lrc文件if (fileUtil.isTxtFile(fileName)) {hashMap.put("FileName", fileName);hashMap.put("FileImage", fileUtil.isWhichImage(fileName));}// 不是此类文件else {hashMap.put("FileName", fileName);hashMap.put("FileImage", R.drawable.file_unknown);}}fileList.add(hashMap);//把数据放到list}simpleAdapter = new SimpleAdapter(//生成适配器FileManagerActivity.this, fileList, R.layout.listview_file,new String[] { "FileImage", "FileName" }, new int[] {R.id.fileimage, R.id.filename });
simpleAdapter.notifyDataSetChanged();//删除一行操作后行提示ListView进行更新 这样更新或耗费一下资源							listView = (ListView) findViewById(R.id.listviewfm);// 设置listView的适配器listView.setAdapter(simpleAdapter);}




另一种更新:删除一行的时候把那一行的position记录下来,然后list.remove(position);然后

filelist.remove(position);//选择行的位置
simpleAdapter.notifyDataSetChanged();
listView.invalidate();

这样应该效率高一些,但是对于只有10来行的ListView效率看不出来许多


  相关解决方案