tags:

views:

171

answers:

1

Hello all,

I am a newb to Android and Java and want to write a funtion that will display a list based on a varable 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  A: 

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 !

Karan
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.
dweebsonduty