Abh*_*ari 3 file-io android listview
在我的文件夹中的文件我已经给出路径并在列表视图中显示但我想在列表文件中显示其大小.我通过这段代码给你你的代码片段我在列表视图中获取所有文件名但我想文件名在列表中的大小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);
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
这里是我的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" />
Run Code Online (Sandbox Code Playgroud)
我想在子项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];
}
Run Code Online (Sandbox Code Playgroud)
在你的代码中
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() + " " + readableFileSize(list[i].length()));
}
Run Code Online (Sandbox Code Playgroud)