Android 读取目录中的文件列表并展示
来源:互联网 发布:水晶球软件 储量计算 编辑:程序博客网 时间:2024/05/21 15:29
根据客户的需求,今天安卓应用中要加一个功能,打开指定目录,并展示其中的文件列表,选中其中一个,就打开,目录如下:
这个需求比较笼统,并且包含的细节也比较多,因此分布实现:
一. 布局文件
ScrollView滚动列表:dialog_spec_item.xml
<ScrollView android:layout_width="fill_parent"android:layout_height="fill_parent"android:scrollbars="vertical"android:fadingEdge="vertical"android:background="#EDEDED"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:scrollbars="vertical"> <ListView android:id="@+id/spec_item_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:dividerHeight="1px" android:divider="#B8B8B8" > </ListView> </LinearLayout> </ScrollView>
列表项布局:spec_item_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/spec_item_seq" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" style="@style/SampleQueryListText" android:gravity="center" android:padding="4dp"/> <View android:layout_width="0.5px" android:layout_height="fill_parent" android:background="#B8B8B8" android:visibility="visible"/> <TextView android:id="@+id/spec_item_name" android:layout_width="0dp" android:layout_weight="9" android:layout_height="wrap_content" style="@style/SampleQueryListText" android:padding="4dp"/> </LinearLayout>
初始化布局文件:
private AlertDialog dlgSpecItem;private View specItemView;
//项目操作说明书对话框specItemView = factory.inflate(R.layout.dialog_spec_item, null);lv = (ListView) specItemView.findViewById(R.id.spec_item_list);
二. 读取目录中的文件列表
//打开指定目录,显示项目说明书列表,供用户选择PATH = Environment.getExternalStorageDirectory() + "/"; File specItemDir = new File(PATH + DT.APP_FOLDER + "/SPEC_ITEM");if(!specItemDir.exists()){specItemDir.mkdir();} if(!specItemDir.exists()){AppUtil.alert(UI.operation_specification_folder_not_found, getActivity());}else{//取出文件列表:final File[] files = specItemDir.listFiles();
List<HashMap<String, Object>> specs = new ArrayList<HashMap<String,Object>>();int seq = 0;for(File spec : files){ seq++;HashMap<String, Object> hashMap = new HashMap<String, Object>(); hashMap.put("seq", seq);hashMap.put("name", spec.getName()); specs.add(hashMap); } SimpleAdapter adapter =new SimpleAdapter( getActivity(), specs, R.layout.list_item_spec, new String[]{"seq","name"}, new int[]{R.id.spec_item_seq, R.id.spec_item_name}); lv.setAdapter(adapter);AppUtil.setListViewHeightBasedOnChildren(lv);//自定义,调整布局,防止只显示第一条数据的问题lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int position, long aid) {String filePath = files[position].getAbsolutePath();Intent intent = getWordFileIntent(filePath); startActivity(intent);}});
四. 打开窗口
//获得自定义对话框if(dlgSpecItem != null){dlgSpecItem.setView(null);}if(specItemView.getParent() != null){((FrameLayout)specItemView.getParent()).removeView(specItemView);}dlgSpecItem = new AlertDialog.Builder(getActivity()).setView(specItemView).setNegativeButton(UI.close, null).show();dlgSpecItem.setCanceledOnTouchOutside(false);
文件列表效果:
打开一个其中一个文件:
/** * 获取打开word文件的intent */public static Intent getWordFileIntent( String param ){ Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(param )); intent.setDataAndType(uri, "application/msword"); return intent; }
/** * 解决ListView在ScrollView中只能显示一行数据的问题 * @param listView*/public static void setListViewHeightBasedOnChildren(ListView listView) {// 获取ListView对应的AdapterListAdapter listAdapter = listView.getAdapter();if (listAdapter == null) {return;}int totalHeight = 0;for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目View listItem = listAdapter.getView(i, null, listView);listItem.measure(0, 0); // 计算子项View 的宽高totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));// listView.getDividerHeight()获取子项间分隔符占用的高度// params.height最后得到整个ListView完整显示需要的高度listView.setLayoutParams(params);}
0 0
- Android 读取目录中的文件列表并展示
- 【php】读取"文件列表"按时间倒序显示,并递归显示各层目录、!
- 读取文件夹中的文件列表
- 读取目录下的文件列表
- php读取目录生成文件列表
- android怎么读取项目中的图片或文件目录分析
- php读取目录中的文件
- 读取assets目录中的文件
- Android 读取Assets资源文件或目录,并拷贝到其他目录下
- listview+baseadapter 读取sd卡中的目录 并修改文件保存文件
- <Android>读取SDCard中的文件并点击响应
- Python读取文件目录并检索
- 如何解析assets目录下的xml文件,,并展示
- Geotools读取shp文件并在Ol2中展示
- VC++ 文件读取并在CListControl上展示
- 利用RODBC包读取并展示xls文件内容
- linux 打印当前所在目录,并展示当前内目录列表
- java读取某个目录下所有文件并通过el表达式将相关文件信息展示出来,js提供页面搜索及查看下载功能
- 支付宝投诉人工QQ号码是
- leetcode笔记:Pascal's Triangle II
- URL中“#” “?” &“”号的作用
- dll问题
- protocol与delegate
- Android 读取目录中的文件列表并展示
- java面试笔记3
- node使用本地存储的方式可以使用多个data本地域
- Using Databases with Python -Musical Track Database (produce database from XML file)
- python 中的yield
- Hashmap原理
- QT 编译 undefined reference to `vtable for Class
- 聚合函数与分组
- 高级并发编程学习-callable与Future的使用