I'm trying to populate a list of files, and I'm using a custom ArrayAdapter so that each list item contains multiple Views. The code I use to populate the list is:
File home = MEDIA;
int numFiles = home.listFiles().length;
if(numFiles >0) {
ArrayList<FileInfo> fileData = new ArrayList<FileInfo>(numFiles);
for (File file : home.listFiles()) {
FileInfo data = new FileInfo();
data.title = file.getName();
data.desc = file.getAbsolutePath();
fileData.add(data);
}
FileAdapter fileAdapter = new FileAdapter(this, R.layout.file_item, fileData);
setListAdapter(fileAdapter);
}
The main XML file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No files found on SD Card."/>
</LinearLayout>
And the file_item XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/file_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/file_item_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Any time I run this and it tries to populate the list, it throws an exception and crashes the application, throwing the error
E/AndroidRuntime( 5320): FATAL EXCEPTION: main
E/AndroidRuntime( 5320): java.lang.RuntimeException: Binary XML file line #6: You must supply a layout_width attribute.
E/AndroidRuntime( 5320): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
E/AndroidRuntime( 5320): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3600)
E/AndroidRuntime( 5320): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3680)
If I add in an additional LinearLayout or something to the file_item XML file, the line that is missing the layout_width will change, which makes me think that it does believe that that first TextView in the file_item XML file is missing a layout_width attribute, but since there's one there already, I have no idea what it wants me to do.