4

I'm trying to create an array of objects with a for loop in Android. The array contains a string taken from a database and an image (for the ease of this I have kept the image to the same one throughout).

I started with the following (which does work):

ItemData[] itemsData = {
                new ItemData(dbString[0], R.mipmap.ic_launcher),
                new ItemData(dbString[1], R.mipmap.ic_launcher),
                new ItemData(dbString[2], R.mipmap.ic_launcher),
                new ItemData(dbString[3], R.mipmap.ic_launcher),
                new ItemData(dbString[4], R.mipmap.ic_launcher)
        };

I want to create the above within a for loop so that when the number of rows in the database changes then the number of objects created will increase without having to amend the code every time.

I have tried several different implementations and the closest I have got is the following (variable b is the number of rows in the database and dbString[i] is the "Name" field in the row):

ItemData[] itemsData = new ItemData[0];
for(int i = 0;i < b;i++) {
    ItemData[i] = new ItemData[]{
        new  ItemData(dbString[i], R.mipmap.ic_launcher)
    };
}

However this still does not work. The only error being bought up is that there is an expression expected at ItemData[i] on line 3 above.

ItemData is being passed to an adapter to then produce a recyclerview of cards.

I am fairly new to programming in general and have researched this issue but am coming up short with an answer that works.

4
  • 2
    Create same size according to items want to Array :ItemData[] itemsData = new ItemData[b]; Commented Feb 20, 2015 at 12:11
  • I thought of this as I was typing my question and tried, Although it was apart of the issue it didn't solve it immediatley. Thank you. Commented Feb 20, 2015 at 12:21
  • +1 for well explained question. But for future reference, this is more of a general Java question than Android specific. You surely find more information if search and read about Java programming on similar topics. Commented Feb 20, 2015 at 12:26
  • To be honest I'm trying to learn both in tandem because I have an idea I wnt to implement as quickly as possible and its given me the kick up the backside to get learning. Probably not the best idea trying to deal with both. Commented Feb 20, 2015 at 14:03

1 Answer 1

2

Once try like this

ItemData[] itemsData = new ItemData[b];
for(int i = 0;i < b;i++) {
itemsData[i] = new  ItemData(dbString[i], R.mipmap.ic_launcher);
}

Hope this will helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly and so simple. I don't know why I didn't see it. Thank you for the assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.