shaneburgess
shaneburgess

Reputation: 15882

Passing variable string to create arrays (Android)

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

Upvotes: 0

Views: 865

Answers (1)

Karan
Karan

Reputation: 12782

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 !

Upvotes: 2

Related Questions