Reputation: 731
I have requirement where based on the certain condition only, i need to initialize the array of type class. So i'm trying insert switch statement inside the array of type class as below.
for (int i=0;i <testChildData.size();i++ )
{
switch (testChildData.get(i)) {
SyncPreferenceItem[] syncCategoryList = {
case "VISIT":
new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS);
break;
case "CUSTOMERS":
new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS);
};
}
}
But i'm getting an error. Could you please point me in the right direction or any other logic for the same. Thank you
Upvotes: 0
Views: 101
Reputation: 24157
The structure is wrong as correct structure would be:
SyncPreferenceItem[] syncCategoryList = new SyncPreferenceItem [testChildData.size];
for (int i=0;i <testChildData.size();i++ ) {
switch (testChildData.get(i)) {
case "VISIT":
syncCategoryList[i] = new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS);
break;
case "CUSTOMERS":
syncCategoryList[i] = new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS);
break;
}
}
There are following points worth noting:
SyncPreferenceItem
.testChildData.size
.ArrayList
.VISIT
and CUSTOMERS
. If anything else goes in there needs to be some code to handle that default case as well.Upvotes: 1
Reputation: 13402
Assumption: For each value you will add the object of SyncPreferenceItem
.
You can add a break
statement after the second case statement. Even though it is not a requirement here, because you don't have anything else after that case statement. But can save you from future errors.
Declare and initialize array outside for loop and add the object using switch.
syncCategoryList = new SyncPreferenceItem[testChildData.size()];
for (int i=0;i <testChildData.size();i++ ) {
switch (testChildData.get(i)) {
case "VISIT":
syncCategoryList[i] = new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS);
break;
case "CUSTOMERS":
syncCategoryList[i] = new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS);
break;
}
}
If you are not sure how many object you are going to create inside the for loop then use ArrayList instead of simple array of SyncPreferenceItem
;
List<SyncPreferenceItem> syncCategoryList = new ArrayList<>();
for (int i=0;i <testChildData.size();i++ ) {
switch (testChildData.get(i)) {
case "VISIT":
syncCategoryList.add(new SyncPreferenceItem(R.drawable.sync_visit, R.string.PrefVisits,
SynchronizationManager.SYNC_CATEGORY_TYPE.VISITS));
break;
case "CUSTOMERS":
syncCategoryList.add(new SyncPreferenceItem(R.drawable.sync_customer, R.string.Customers,
SynchronizationManager.SYNC_CATEGORY_TYPE.CUSTOMERS));
break;
}
}
Upvotes: 3