android– 如何使用文件名在列表视图中显示文件大小

android– 如何使用文件名在列表视图中显示文件大小,第1张

概述在我的文件夹中的文件我已经给出路径并在列表视图中显示但我想在列表文件中显示其大小.我通过这段代码给你你的代码片段我在列表视图中获取所有文件名,但我想在列表中的文件名及其大小plz帮助我.Stringroot_sd=Environment.getExternalStorageDirectory().toString();fil

在我的文件夹中的文件我已经给出路径并在列表视图中显示但我想在列表文件中显示其大小.我通过这段代码给你你的代码片段我在列表视图中获取所有文件名,但我想在列表中的文件名及其大小plz帮助我.

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" />

我想在子项plz中显示文件大小给我解决我的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 – 如何使用文件名在列表视图中显示文件大小全部内容,希望文章能够帮你解决android – 如何使用文件名在列表视图中显示文件大小所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1116412.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存