android - 如何在带有文件名的 ListView 中显示文件大小

标签 android listview file-io

我的文件夹中的文件我已经给出了路径并在 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/

相关文章:

android - Retrofit 2 模拟最佳实践

c - 在另一个文件中搜索一个文件的一部分

android - Android如何防止用户提取应用程序的APK?

android - 接近警报是否需要从我的应用程序更新坐标?

android - 从 Android 上新的 Google 相册应用程序获取照片和视频

android - 更改 ListView 中第一项的背景颜色

C#:获取文件大缩略图/预览,而不是大图标,使用 SHGetImageList 和 SHGetFileInfo,如 Windows 资源管理器

android - 在 ListView 的选定项目中显示复选标记

c - c - 如何在文件中写入两个字符串,用空格或逗号分隔?

c++ - C++ 中的程序完美读取一个文件,但在读取另一个文件时添加 "∩╗┐",但是两个文件的内容相同。如何?