Reputation: 268
I want to pass in string resource value in string array but it doesn't take value in array. it will take null value
Below is my code :
prepaid = getResources().getString(R.string.lbl_prepaid);
dth = getResources().getString(R.string.lbl_dth);
public String[] addedRTHomeList(){
String[] homeList = {prepaid,dth,"PostPaid","Utility Services","Complaint Entry","Complaint Status","Recharge Status",chat,"Topup Request","Redeem Discount","Private Bus Booking","ST Bus Booking","Hotel Booking","Flight Booking","Money Transfer","Settings","Reports","DTH Activation"};
return homeList;
}
for(int i = 0; i < ba.addedRTHomeList().length ; i++)
{
md = new MenuDetail();
md.setMenuName(ba.addedRTHomeList()[i]);
md.setImageId(ba.RTDrwableListThemeRed()[i]);
RTmenuListRed.add(md);
RTMenuThemeRed.put(ba.RTMenuCode()[i],RTmenuListRed);
}
In for loop when i access 1st value of addedRTHomeList array it take null value
Upvotes: 0
Views: 1091
Reputation: 6828
You are getting an exception because you are trying to initialize the variables as class variables. I moved the initialization inside addedRTHomeList()
method and found no issues.
Try this code,
public String[] addedRTHomeList(){
String prepaid = getResources().getString(R.string.lbl_prepaid);
String dth = getResources().getString(R.string.lbl_dth);
String chat = getResources().getString(R.string.lbl_chat);
return new String[]{prepaid,dth,"PostPaid","Utility Services","Complaint Entry","Complaint Status","Recharge Status",chat,"Topup Request","Redeem Discount","Private Bus Booking","ST Bus Booking","Hotel Booking","Flight Booking","Money Transfer","Settings","Reports","DTH Activation"};
}
String[] items = ba.addedRTHomeList();
for(int i = 0; i < items.length ; i++){
md = new MenuDetail();
md.setMenuName(items[i]);
md.setImageId(ba.RTDrwableListThemeRed()[i]);
RTmenuListRed.add(md);
RTMenuThemeRed.put(ba.RTMenuCode()[i],RTmenuListRed);
}
Upvotes: 0
Reputation: 8149
Use AarryList instead of simple array becasue arraylist have easy dynamic implementation.
When you create array you have tell him its length.
public String[] addedRTHomeList(){
String[] homeList = {prepaid,dth,"PostPaid","Utility Services","Complaint Entry","Complaint Status","Recharge Status",chat,"Topup Request","Redeem Discount","Private Bus Booking","ST Bus Booking","Hotel Booking","Flight Booking","Money Transfer","Settings","Reports","DTH Activation"};
return homeList;
}
Like Above homeList is size of 17 you cant add value in 18 index because 18 is not exist that why you are getting this error
Solution:
Use String ArrayList example usage
ArrayList<String> homeList = new ArrayList<String>();
homeList.add("PostPaid");
homeList.add("Complaint Status");
homeList.add("Recharge Status");
.
.
}
to get Value from ArrayList homeList.get(index)
Simply.
now if you add value it will create new index and add it on last. Best Of Luck.
Upvotes: 0
Reputation: 4335
Instead of this you can create <string-array>
in your string.xml
. like,
<string-array name="your_array_name">
<item>Element 1</item>
<item>Element 2</item>
<item>Element 3</item>
<item>Element 4</item>
.
.
.
</string-array>
after this you can get in Java as,
String[] mTestArray = = getResources().getStringArray(R.array.your_array_name);
this may helps your problem.
Upvotes: 1