Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence CRUD Operations with SQLite Retrieving Data

Does your object *need* parameters?

Evan Anger I'm applying what is taught in these videos into my own app, but when I got to this part:

        ArrayList<ListItem> list = new ArrayList<ListItem>();
        if (cursor.moveToFirst()) {
            do {
                ListItem listItem = new ListItem(getIntFromColumnName(cursor, BaseColumns._ID),
                        getStringFromColumnName(cursor, EasyListSQLiteHelper.LISTS_NAME),
                        getStringFromColumnName(cursor, EasyListSQLiteHelper.LISTS_CHECKED),
                        null);
                list.add(listItem);
            }while(cursor.moveToNext());
        }

My 'ListItem' got an error as expected because it doesn't have any parameters. It only has two values: an int and string value (getters and setters for those too). So how would I go about reading my two values this way?

Also, do I need to put ListItem into the angle brackets after 'new'?

1 Answer

    private ArrayList<TaskItem> mListsList; // The list's array of tasks
    private String mListName; // The name of the list
    private int mImportant; // Whether the item is important

    public ListItem() {}

    public ArrayList<TaskItem> getListsList() {
        return mListsList;
    }

    public void setListsList(ArrayList<TaskItem> listsList) {
        mListsList = listsList;
    }

    public String getListName() {
        return mListName;
    }

    public void setListName(String listName) {
        mListName = listName;
    }

    public int getImportant() {
        return mImportant;
    }

    public void setImportant(int important) {
        mImportant = important;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(mListName);
        parcel.writeTypedList(mListsList);
        parcel.writeInt(mImportant);
    }

    protected ListItem(Parcel in) {
        mListName = in.readString();
        mListsList = in.createTypedArrayList(TaskItem.CREATOR);
        mImportant = in.readInt();
    }

    public static final Creator<ListItem> CREATOR = new Creator<ListItem>() {
        @Override
        public ListItem createFromParcel(Parcel source) {
            return new ListItem(source);
        }

        @Override
        public ListItem[] newArray(int size) {
            return new ListItem[size];
        }
    };
Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

It looks like the problem is that your ListItem constructor doesn't have any parameters, but you're passing it some in your original code snippet.

It looks like you're trying to create a ListItem object for each record returned in a query. However, not all of the query results (_ID, LISTS_NAME, and LISTS_CHECKED) match the ListItem class fields (mListsList, mListName, and mImportant). LISTS_NAME is probably mListName and LISTS_CHECKED might be mImportant, but it doesn't look like you have a column in your query for mListsList.

Either way, if you're wanting to create a ListItem object from your query results, you're going to need to have a ListItem constructor like this:

public ListItem (List<TaskItem> listsList, String listName, int important) {
    mListsList = listsList;
    mListName = listName;
    mImportant = important;
}

Does that help some?

I don't know why some of the text in my answer is underlined - that's unintentional so please disregard it.

That's better, thank you!