0

I am a newbie to Android and Java and want to write a function that will display a list based on a variable that I pass to the function.

The function is below and the code below creates an array out of a string called type, but what I want to do is pass it a variable string and have it build a list based on that string.

So if I wanted the type list I would say list_it("type")

But if I try something like getResources().getStringArray(R.array.thelist); it doesn't work.

Can someone point me in the right direction?

public void list_it(String thelist){
    String[] types = getResources().getStringArray(R.array.type);
      ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, R.layout.list_item1, types);
      setListAdapter(mAdapter);
      ListView lv = getListView();
      lv.setTextFilterEnabled(true);
}

1 Answer 1

2

Use the following code to get the identifier for the given name i.e thelist :

int resID = getResources().getIdentifier( thelist, "string", "<package name>" );

This will return you the identifier for the given resource name. Then use the

getResources().getStringArray( resID );

HTH !

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

1 Comment

Thanks alot, this worked great! Since I am so new at this, I was not sure where to get the type("string") from then I looked in R.java and got it from there. In case any other newbs see this. Thanks Again.

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.