0

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

4
  • Why you don't want to use <string-array> resource? Commented Jul 15, 2016 at 5:18
  • You cannot to this homeList = {prepaid,dth} you can only combine using Array.combine Commented Jul 15, 2016 at 5:19
  • Thanx .. if possible please share example.. Commented Jul 15, 2016 at 5:22
  • hey michael in my case ..one array value set after web service response so i do not use string-array resource Commented Jul 15, 2016 at 6:27

3 Answers 3

1

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.

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

Comments

0

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.

Comments

0

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);
}

1 Comment

all that value which have refernce like prepaid,dth,chat

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.