0

I'm just getting into android app development. I'm trying to find the value of an item in an array resource from the index, but the resource is returning an integer rather than an array. I have this piece of code:

                pop1.setItems(R.array.PopupMenuListItems, new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which ) {
                    Toast.makeText(getBaseContext(), "Selected " + R.array.PopupMenuListItems, Toast.LENGTH_SHORT).show();
                }
            });

and when the Toast message shows, it shows "Selected 2131492864", and I can't index an integer.

So, my question is how can I convert R.array.PopupMenuListItems into an array rather than an integer, so I can use R.array.PopupMenuListItems[which]?

2 Answers 2

3

Use getStringArray for getting Array from resources :

 public void onClick(DialogInterface dialog, int which ) {
 String[] arrAllData = getResources().getStringArray(R.array.PopupMenuListItems2);
  Toast.makeText(getBaseContext(), 
                 "Selected " + arrAllData[which], Toast.LENGTH_SHORT).show();
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, will make as answer in 10 minutes.
@BraydonDavis: One more suggestion: we can also pass String Array to setItems method so instead of reading Array from resources again and again read it only one time before pop1.setItems line and use in both places
1

Access like this way,

 String[]  mTestArray =  = getResources().getStringArray(R.array.testArray); 

Comments

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.