Android - Loop Through strings.xml文件

时间:2012-08-30 08:42:03

标签: java android xml loops

我想知道是否还有循环遍历strings.xml文件。

假设我有以下格式:

<!-- FIRST SECTION -->
<string name="change_password">Change Password</string>
<string name="change_server">Change URL</string>
<string name="default_password">password</string>
<string name="default_server">http://xxx:8080</string>
<string name="default_username">testPhoneAccount</string>

<!-- SECOND SECTION -->
<string name="debug_settings_category">Debug Settings</string>
<string name="reload_data_every_startup_pref">reload_data_every_startup</string>
<string name="reload_data_on_first_startup_pref">reload_data_on_first_startup</string>

现在让我说我有这个:

private HashMap<String,Integer> hashmapStringValues = new HashMap<String, Integer>();

有没有办法只在我的xml文件的第二部分进行迭代?也许用<section2>之类的标签包装该部分,然后迭代它?

public void initHashMap(){
    for (int i=0;i< ???? ;i++) //Here I need to loop only in the second section of my xml file
    {          
        String nameOfTag = ? // Here I get the name of the tag
        int value = R.string.nameOfTag // Here I get the associated value of the tag

        this.hashmapStringValues.put(nameOfTag,value);
    }
}

4 个答案:

答案 0 :(得分:4)

不,但您可以在包含以下内容的资源/值中创建另一个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="array">
        <item>ID_1|Asdf</item>
        <item>ID_2|I do not like to think</item>
        <item>ID_3|Whatever</item>
    </string-array>

</resources>

您可以根据需要格式化字符串,然后使用自定义解析器解析它们。

要获取字符串数组,您只需要这样做:

getResources().getStringArray(R.array.array);

答案 1 :(得分:2)

如果您查看生成的android.R java文件,它会让您知道如何通过Reflection实现此目的:

    Field fields[] = R.string.class.getFields();
    for (Field field : fields) {
        Log.d("appname", field.getName() + ":" + getResources().getIdentifier(field.getName(), "string", this.getPackageName()));
    }

注意,我不会经常这样做,但一旦App加载应该没问题。

答案 2 :(得分:0)

这是不可能的,因为基本的java本身没有检测到评论

您只能选择这样的字符串。

getResources().getString(R.string.app_name)

答案 3 :(得分:0)

public class MainActivity extends AppCompatActivity{
// one way to iterate over the arrays in the strings.xml file
// is to name them all here......
String[] categories = {"millis","size_uk","size_us","size_jp"};
ArrayList<Spinner> spinners = new ArrayList<>();
ArrayAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // iterate over the views in the layout and find the spinners
    // need to iterate over spinners separately
    // because need to identify them individually
    // in order to set their selection in the event handlers
    // of the other spinners
    GridLayout gridLayout = findViewById(R.id.GridLayout1);
    for (int i = 0; i < gridLayout.getChildCount(); i++) {
        View view = gridLayout.getChildAt(i);
        // if the view is a spinner.....
        if (view instanceof Spinner){
            spinners.add((Spinner)view);
        }
    }
    // it is an array of arrays
    // iterate over the arrays and fill each spinner
    // they used to be called strange objects
    // plus attach an event to each spinner
    for (int i = 0; i < categories.length; i++){
        Spinner spinner = spinners.get(i);
        // identify the array from it's name in the resource file
        int id = this.getResources().getIdentifier(categories[i], "array",
                this.getPackageName());
        // create an adapter from said array
        adapter = ArrayAdapter.createFromResource(this, id,
                android.R.layout.simple_spinner_item);
        //put the contents of the array in the spinner
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        //set listener event for each spinner
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // set all spinners to the same index
                for(int k = 0; k < 4; k++)
                    spinners.get(k).setSelection(position);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // do nothing
            }
        });
    }
}

}