如何创建与字符串数组名称关联的动态变量?

时间:2014-04-28 13:24:55

标签: android

有一些字符串数组,它们是微调器的数据源:

<?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>

在区域微调器的onItemSelected中,我想根据项目选择位置获得R.array.districts_region0或R.array.districts_region1。我想写这样的东西:

@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) {
            ...
        }
    }

但这会崩溃!那么如何动态设置spinnerDistrict的适配器?

3 个答案:

答案 0 :(得分:1)

您需要找到string-array这样的资源ID

String tag = "districts_region"+pos;

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

然后,

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

答案 1 :(得分:0)

尝试使用这样的辅助方法:

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

    return resID;
}

你会像这样使用它:

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

答案 2 :(得分:0)

您可以使用这样的自定义Adapter

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

ExampleAdapter将循环显示在顶部定义的字符串,并将其分配给TextView - 在本例中为R.id.tvText - 在列表项内。