Search value for key in string-array android

后端 未结 8 1387
我在风中等你
我在风中等你 2020-12-09 05:48

i have a string-array in my res/values/strings.xml

   
    Item1
    Item2         


        
8条回答
  •  自闭症患者
    2020-12-09 06:19

    I had the same problem. The decision for me was to create many strings in xml-file (not string arrays) and to create String[] array in code. It looks like this:

        Builder builder = new AlertDialog.Builder(DMCBrowser.this);
        builder.setTitle(R.string.title_playlist);
        final CharSequence[] items = new CharSequence[] { getResources().getString(R.string.watch_all),
                            getResources().getString(R.string.select_items) };
        builder.setItems(items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (items[which].equals(getResources().getString(R.string.watch_all))) {
                                Log.d(TAG, "Watch all");
                            } else if (items[which].equals(getResources().getString(R.string.select_items))) {
                                Log.d(TAG, "Select items");
                            }
                        }
                    }).show();
    

    Although it does not look much compact, we can differ one item from another not only by non-understandable identifier like 1 or 2, but by human-readable android R-id. If i would like to change item order, it will be very easy.

提交回复
热议问题