我的文件夹中的文件我已经给出了路径并在 ListView 中显示,但我想在列表文件中显示它的大小。我通过这段代码给你代码 fragment 我在 ListView 中获取所有文件名但我想在列表中找到文件名及其大小请帮助我。
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/recycle/" ) ;
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList ));
setContentView(R.layout.main);
mFilePathTextView = (TextView)findViewById(R.id.file_path_text_view);
mStartActivityButton = (Button)findViewById(R.id.start_file_picker_button);
mStartActivityButton.setOnClickListener(this);
protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id);
final File temp_file = new File( file, myList.get( position ) );
AlertDialog alertbox = new AlertDialog.Builder(this)
.setTitle("File Detail")
.setMessage("File Name:"+temp_file)
.setPositiveButton("Delete",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
DeleteRecursive(temp_file);
refresh();
//mylist.invalidateViews();
//adapter.notifyDataSetChanged();
}
})
.setNegativeButton("Restore", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
String s5=temp_file.toString();
String z1[]={"Field1","Field2"};
try
{
String from= temp_file.toString();
这是我的 xml 文件************ *************** ***
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#aa0000"
android:drawSelectorOnTop="false" />
我想在子项目中显示文件大小请给我解决方案,我的 xml 文件和代码有什么变化。
最佳答案
这样做
这个方法会计算文件大小
public String readableFileSize(long size) {
if (size <= 0)
return "0";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
在你的代码中
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() + " " + readableFileSize(list[i].length()));
}
关于android - 如何在带有文件名的 ListView 中显示文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20112274/