标签:
扫描本地文件加载到item
实现代码如下:
GetListActivity如下:(主Activity)
package com.iwanghang.sdcardsearchtolist; import android.os.Bundle; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; /** * 不要忘记加权限 * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> */ public class GetListActivity extends Activity { private Button myBtn = null; private ListView lv = null; private SimpleAdapter sa = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_list); lv = (ListView)findViewById(R.id.listv); String[] from = {"text_content"}; int[] to = {R.id.myView1}; sa = new SimpleAdapter(this, getList(), R.layout.itemlist, from, to); myBtn = (Button)findViewById(R.id.myButton); myBtn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub lv.setAdapter(sa); } }); } public List<Map<String, Object>> getList() { List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); String sDStateString = Environment.getExternalStorageState(); if(sDStateString.equals(Environment.MEDIA_MOUNTED)) { //File SDFile = Environment.getExternalStorageDirectory(); //File sdPath = new File(SDFile.getAbsolutePath()); File sdPath = new File(Environment.getExternalStorageDirectory() + "/drm_music"); System.out.println("GetListActivity.List.sdPath = " + sdPath ); System.out.println("GetListActivity.List.listFiles = " + sdPath.listFiles() ); if(sdPath.listFiles().length > 0) { for(File file : sdPath.listFiles()) { Map<String, Object> map = new HashMap<String, Object>(); map.put("text_content", file.getName()); list.add(map); System.out.println("name-->"+file.getName()); } } } return list; } //其中,getList()用于扫描本地文件,获得文件的名字;为ListView设置一个适配器,将扫描到的文件显示在ListView上,这里需要注意,适配器的后面两个参数都是数组。 }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.iwanghang.sdcardsearchtolist.GetListActivity" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="获取本地文件信息" /> <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="扫描" /> <ListView android:id="@+id/listv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lvitem" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns ="*"> <TableRow> <TextView android:id="@+id/myView1" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/myView2" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TextView> <TextView android:id="@+id/myView3" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </TableRow> </TableLayout>
Android开发本地及网络Mp3音乐播放器(十八)新下载歌曲加载准备SdcardSearchToList
标签:
原文地址:http://blog.csdn.net/iwanghang/article/details/51395664