就是网上安卓变量sd卡搜索文件的代码
上代码:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import java.util.*;
import android.widget.*;
import java.io.*;
public class MainActivity extends Activity
{
private String keywrod = "txt";
private File files;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
files = new File("/sdcard/");
SimpleAdapter saImageItems = new SimpleAdapter(this,
getData(),// 数据来源
R.layout.user,//每一个user xml 相当ListView的一个组件
new String[] { "name", "age" },
// 分别对应view 的id
new int[] { R.id.name, R.id.age });
// 获取listview
((ListView) findViewById(R.id.users)).setAdapter(saImageItems);
}
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
private List<Map<String, Object>> getData()//获取数据集给予list
{
return list;
}
private void search(File fileold)
{
try{
File[] files=fileold.listFiles();
if(files.length>0)
{
HashMap<String, Object> user = new HashMap<String, Object>();
for(int j=0;j<files.length;j++)
{
if(!files[j].isDirectory())
{
if(files[j].getName().indexOf(keywrod)> -1)
{
// path += "\n" + files[j].getPath();
//result.setText(info+path);
user.put("age",files[j].getName());
user.put("name",files[j].getPath());
list.add(user);
//shuju.putString(files[j].getName().toString(),files[j].getPath().toString());
}
}
else{
this.search(files[j]);
}
}
}
}
catch(Exception e)
{ }
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="用户列表"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#DAA520"
android:textColor="#000000">
</TextView>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/users"
android:background="#D07873">
</ListView>
user.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
<TableRow > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img"> </ImageView> <TextView android:layout_height="wrap_content" android:layout_width="150px" android:id="@+id/name"> </TextView> <TextView android:layout_height="wrap_content" android:layout_width="170px" android:id="@+id/age"> </TextView> </TableRow>