android browse files code

2019-06-09 03:14发布

问题:

In the following code i am trying to implement the follwwing browser code http://www.remwebdevelopment.com/dev/a34/Directory-Browser-Application.html

I am just getting a blank text view here as the ouptut and i do not see the browser.What am i doing wrong here

 public class File_browse extends Activity {
private List<String> items = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file_browse);
    getFiles(new File("/").listFiles());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_file_browse, menu);
    return true;
}

protected void onListItemClick(ListView l, View v, int position, long id){
    int selectedRow = (int)id;
    if(selectedRow == 0){
        getFiles(new File("/").listFiles());
    }else{
        File file = new File(items.get(selectedRow));
        if(file.isDirectory()){
            getFiles(file.listFiles());
        }else{
             new AlertDialog.Builder(this)
             .setTitle("This file is not a directory")
             .setNeutralButton("OK", new DialogInterface.OnClickListener(){
                 public void onClick(DialogInterface dialog, int button){
                     //do nothing
                 }
             }).show();
            //Toast.makeText(this, "This file is not a directory" + position, Toast.LENGTH_SHORT).show();
        }
    }
}
private void getFiles(File[] files){
    Toast.makeText(this, "In get files" , Toast.LENGTH_SHORT).show();
    items = new ArrayList<String>();
    items.add(getString(R.string.app_name));
    for(File file : files){
        items.add(file.getPath());
    }
    ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,R.layout.file_list_row, items);
    ArrayAdapter adp = new ArrayAdapter(File_browse.this, android.R.layout.simple_list_item_1);
    ListView mainlist = null;
    mainlist.setAdapter(adp);
    mainlist.setTextFilterEnabled(true);

}




 }

Main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <ListView android:id="@android:id/list"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

<!-- <TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:padding="@dimen/padding_medium"
    android:text="@string/hello_world"
    tools:context=".File_browse" />-->


 </RelativeLayout>

file_list_row.xml is next to activity_file_browse.xml

 <?xml version="1.0" encoding="utf-8"?>
 <TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

回答1:

1/ mainList is null, so it most likely crashes

2/ The adapter fileList is never used, so nowhere you actually use the list of file names in an adapter

3/ adp doesn't contain anything as you don't give it any content

4/ given that your list is named @android:id/list, you probably wanted to extend ListActivity?

5/ onListItemClick is never called, nor does it overrides anything (as you don't extend ListActivity), so it is basically dead code

6/ File_browse should be named FileBrowse to respect naming format convention in java.