在下面的代码中,我试图实现以下浏览器代码http://www.remwebdevelopment.com/dev/a34/Directory-Browser-Application.html
我只是在这里得到一个空白文本视图作为输出,我没有看到浏览器。我在这里做错了什么
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);
}
}
主.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 在 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"/>