I have a class that describes an object such that:
data class Item(
var productName: String = "",
var productId: String = "",
var productDesc: String = "",
var productPrice: String = ""
)
and an object which holds a variable of a ListOf Item(s):
object Items {
var myItems = listOf<Item>(
Item(
productName: "Item #1",
productId: "com.example.website.products.item1",
productDesc: "This is a description for Item #1",
productPrice: "$5.99"
),
Item(
productName: "Item #2",
productId: "com.example.website.products.item2",
productDesc: "This is a description for Item #2",
productPrice: "$0.99"
)
)
}
This works great, I can access the List with:
for (myItem in Items.myItems) {
//do this
}
or other similar methods.
What I have run into lately is that the list of items has grown too big and the file is becoming too large for AndroidStudio to process.
I would like to be able to make a few separate objects and then merge them into one object with a reference to each object. Not sure I am explaining this correctly, but this is what I imagine it would look like:
data class Item(
var productName: String = "",
var productId: String = "",
var productDesc: String = "",
var productPrice: String = ""
)
//Master Object with references to "sub" objects below:
object Items {
var myItems = listOf<Item>(
ItemsForUSA.myItems,
ItemsForCAN.myItems
)
}
//Object #1
object ItemsForUSA {
var myItems = listOf<Item>(
Item(
productName: "Item #1",
productId: "com.example.website.products.item1",
productDesc: "This is a description for Item #1",
productPrice: "$5.99"
),
Item(
productName: "Item #2",
productId: "com.example.website.products.item2",
productDesc: "This is a description for Item #2",
productPrice: "$0.99"
)
)
}
//Object #2
object ItemsForCAN {
var myItems = listOf<Item>(
Item(
productName: "Item #3",
productId: "com.example.website.products.item3",
productDesc: "This is a description for Item #3",
productPrice: "$2.99"
),
Item(
productName: "Item #4",
productId: "com.example.website.products.item4",
productDesc: "This is a description for Item #4",
productPrice: "$1.99"
)
)
}
This way, I could reference Items.myItems
and get everything in both ItemsForUSA
and ItemsForCAN
etc..
Is this possible?
Any insight appreciated.
CodePudding user response:
You can combine collections with a.plus(b)
- since that's an operator, you can just do a b
.
Just bear in mind that this produces a new collection, so if you're under memory constraints, this isn't the way to go! And you'd want something like a database instead, so you can select or stream data as needed instead of holding it all in memory.
If it's just a case of your file having a huge number of lines and making Android Studio choke, you might want to consider storing your data in a text file instead (e.g. a CSV file) and place it in assets
. Then you can read from that file at runtime and parse it into Item
s for your list, or use it to initialise a database, etc. It'll be way easier (and more performant) to work with as a plain text file too.
(You're working with List
s by the way, not Array
s! They're not the same thing - there can be some overlap in how you interact with them though, plus
is defined for both)