0

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 ?

2
  • Please post your logcat output. You can create a custom Adapter to do this. Commented Apr 28, 2014 at 13:28
  • logcat is blank , the app crashes just before opening the activity! Commented Apr 28, 2014 at 13:30

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

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.