我正在开发Android的文件管理器。最初的任务是实现一个活动,显示SD卡上的文件夹列表,我设法做到了。
现在我可以在SD卡上看到文件夹列表。但我无法在我的应用程序中打开这些文件夹以查看其中的文件夹。如何实现此功能?
请帮忙!这个问题让我发疯了)
这是我的代码:
public class MainActivity extends Activity {
private ListView mListView;
private List<String> fileNameList;
private FlAdapter mAdapter;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView1);
file = Environment.getExternalStorageDirectory();
fileNameList = getFileListfromSDCard();
mAdapter = new FlAdapter(this, R.layout.list_item, fileNameList);
mListView.setAdapter(mAdapter);
}
private List<String> getFileListfromSDCard() {
String state = Environment.getExternalStorageState();
List<String> flLst = new ArrayList<String>();
if (Environment.MEDIA_MOUNTED.equals(state) && file.isDirectory()) {
File[] fileArr = file.listFiles();
int length = fileArr.length;
for (int i = 0; i < length; i++) {
File f = fileArr[i];
flLst.add(f.getName());
}
}
return flLst;
}
public class FlAdapter extends ArrayAdapter<String> {
private List<String> fLst;
private Context adapContext;
public FlAdapter(Context context, int textViewResourceId,
List<String> fLst) {
super(context, textViewResourceId, fLst);
this.fLst = fLst;
adapContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
FHolder fHolder = null;
if (convertView == null) {
view = View.inflate(adapContext, R.layout.list_item, null);
fHolder = new FHolder();
fHolder.fNameView = (TextView) view.findViewById(R.id.fname);
view.setTag(fHolder);
} else {
fHolder = (FHolder) view.getTag();
}
String fileName = fLst.get(position);
fHolder.fNameView.setText(fileName);
return view;
}
}
static class FHolder {
public TextView fNameView;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/fname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>