0

I'm trying to output a list from an SQLite database and am getting this exception:

android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x102000a

After checking around on similar questions, I've made sure my ListView is defined as @id/android:list but still get the same exception. The problem occurs when the onCreate method in the ListActivity class completes. The getAllDiaryEntries() method does get the data from the db, but again, once it comes back to the ListActivity class and onCreate finishes I get the exception. Here's the relevant parts of code.

This is the ListActivity class:

public class DiarySchedule extends ListActivity
{
private DiaryDataSource datasource;
private static final String TAG = "DiaryDbAdapter";

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diary_schedule);

    datasource = new DiaryDataSource(this);
    datasource.open();

    List<DiaryEntry> values = datasource.getAllDiaryEntries();

    ArrayAdapter<DiaryEntry> adapter = new ArrayAdapter<DiaryEntry>(this,
            android.R.id.list, values);
    setListAdapter(adapter);
}

Next we have the getAllDiaryEntries() method:

public List<DiaryEntry> getAllDiaryEntries() 
{
    List<DiaryEntry> diaryEntries = new ArrayList<DiaryEntry>();

    Cursor cursor = database.query(DiaryDbAdapter.DIARY_TABLE,
            allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) 
    {
        DiaryEntry diaryEntry = cursorToDiaryEntry(cursor);
        diaryEntries.add(diaryEntry);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return diaryEntries;
}

And the layout (not styled or anything yet):

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

I'm still pretty new to Android so may have missed something simple, but thanks in advance for any help.

Edited - here's the cursortoDiaryEntry method

private DiaryEntry cursorToDiaryEntry(Cursor cursor) { DiaryEntry diaryEntry = new DiaryEntry(); diaryEntry.setId(cursor.getLong(0)); diaryEntry.setTitle(cursor.getString(1)); diaryEntry.setDate(cursor.getString(2)); diaryEntry.setDescription(cursor.getString(3)); return diaryEntry; }

2 Answers2

2

The constructor (the one you're using) of the ArrayAdapter is defined as follows:

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)

In here the textViewResourceId is "The resource ID for a layout file containing a TextView to use when instantiating views.". See here.

You should pass a layout file containing a <TextView/> element, using R.layout.layout_file.

dennisg
  • 4,328
  • 1
  • 21
  • 27
  • Thanks dennisg. The exception has now gone, but other problems are arising..! After changing to: 'ArrayAdapter adapter = new ArrayAdapter(this,R.layout.diary_schedule, values);' where 'diary_schedule' is my layout file, an exception popped up wanting an id for a TextView, so I added to it again: 'ArrayAdapter adapter = new ArrayAdapter(this, R.layout.diary_schedule, R.id.scheduleList, values);' where 'schedule_list' is the TextView, and now there are no exceptions but the page is coming up blank.. – Captain Chod Apr 14 '12 at 14:58
  • Ok this is interesting - I just tried changing it to 'ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);' and it output 2 rows, but for both it output the package name and then 'DiaryEntry@412f7728' (DiaryEntry is my class with the getTitle, getDate etc methods). Any ideas? I've blatantly done something really stupid somewhere! – Captain Chod Apr 14 '12 at 15:12
  • It sounds like you made a list of pointers rather than a list of the objects they pointed to. Maybe put up your `cursorToDiaryEntry` method for us to look at. – Barak Apr 14 '12 at 15:28
  • In the first comment you use R.id.scheduleList for the TextView id, but you mention that the TextView's id is 'schedule_list'. In both cases it will try to set the value of TextView to an object DiaryEntry. It calls the toString() method to convert the object to a String (which is the DiaryEntry@412f7728). You could overwrite the toString() method in your class DiaryEntry to print the correct information, but it would better to create a new class that extends ArrayAdapter and then override the getView() method. See an example here: http://stackoverflow.com/a/10153102/1327789. – dennisg Apr 14 '12 at 15:31
  • Ok I've added the cursorToDiaryEntry method above, and I'll try creating a new class that extends ArrayAdapter and let you know how I go on. Thanks again guys! – Captain Chod Apr 14 '12 at 16:00
  • I created the class extending ArrayAdapter and it's now outputting just fine. Thanks so much for your help guys! – Captain Chod Apr 14 '12 at 16:16
1

dennisg has the right answer to fix your problem.

Might I suggest that you are making extra work by turning your cursor into an array? You can save some cycles by using your cursor directly to create your list adapter.

Cursor cursor = database.query(DiaryDbAdapter.DIARY_TABLE,
        allColumns, null, null, null, null, null);
getActivity().startManagingCursor(cursor);

String[] from = new String[] { DiaryDbAdapter.DIARY_ENTRY };
int[] to = new int[] {  R.id.ListItem1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
    R.layout.listlayoutdouble, cursor, from, to);
setListAdapter(adapter);

Your from string[] contains the list of columns you wish to include in the display, the to int[] contains the resource id's you wish to put the data into (in matching order with the from array). Then the adapter callout specifies which row layout to use, what cursor and the two arrays to map the data to the view.

Barak
  • 16,117
  • 9
  • 49
  • 84
  • I tried clicking the up arrow on this Barak, but apparently I don't have a high enough reputation to do that yet! Thanks again though! – Captain Chod Apr 14 '12 at 16:17