pheromix
pheromix

Reputation: 19287

How to create dynamic variable associated with string-array name?

There are string-arrays which are datasources for spinners :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="regions">
        <item>Analamanga</item>
        <item>Diana</item>
    </string-array>
    <string-array name="districts_region0"> // the 0 corresponds to the item at position 0
        <item>Central</item>
    </string-array>
    <string-array name="districts_region1"> // the 1 corresponds to the item at position 1
        <item>Nosy-be</item>
        <item>Sambava</item>
    </string-array>
</resources>

In the onItemSelected of the region spinner I want to get either R.array.districts_region0 or R.array.districts_region1 according to the item selected position. I want to write something like this :

@Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // TODO Auto-generated method stub
        if (parent.getId() == R.id.region) {
            String tag = "districts_region"+pos;
            tag = "R.array."+tag;
            ArrayAdapter<CharSequence> districtAdapter = ArrayAdapter.createFromResource(this,
                    Integer.parseInt(tag), android.R.layout.simple_spinner_item);
            districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerDistrict.setAdapter(districtAdapter);
        } else if (parent.getId() == R.id.district) {
            ...
        }
    }

But this crashes ! So how to set dynamically the adapter of spinnerDistrict ?

Upvotes: 0

Views: 555

Answers (3)

Sheetal Suryan
Sheetal Suryan

Reputation: 495

Try using a helper method like this:

public static int getResIdFromString(Context context, String path){
    int resID = context.getResources().getIdentifier(path, "drawable", context.getPackageName());

    return resID;
}

And you would use it like this:

int rid = Utils.getResIdFromString(context, "icon" + i.toLowerCase());

Upvotes: 0

Libin
Libin

Reputation: 17085

You need to find the resource id of string-array like this

String tag = "districts_region"+pos;

int resourceId = getResources().getIdentifier(tag, "array", getPackageName());

And then,

ArrayAdapter<CharSequence> districtAdapter = ArrayAdapter.createFromResource(this,
                resourceId, android.R.layout.simple_spinner_item);

Upvotes: 1

Xaver Kapeller
Xaver Kapeller

Reputation: 49807

You can use a custom Adapter like this:

private class ExampleAdapter extends ArrayAdapter<String> {

    private final int[] strings = new int[] {
            R.id.string1,
            R.id.string2,
            R.id.string3,
    };

    private ExampleAdapter(Context context, int resource) {
        super(context, resource);
    }

    private ExampleAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public ExampleAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
    }

    private ExampleAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    private ExampleAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    private ExampleAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        int stringResId = getStringForPosition(position);
        TextView tvText = view.findViewById(R.id.tvText);
        tvText.setText(stringResId);

        return view;
    }

    private int getStringForPosition(int position) {
        return strings[position % strings.length];
    }
}

This ExampleAdapter will cycle through the strings defined at the top and assign them to a TextView - in this case R.id.tvText - inside the list items.

Upvotes: 0

Related Questions