There is a CreateView method that dynamically creates a View and fills in fields that appear in the view with data from the SharedPrefernces file.

The first for (File loadFile: loadFiles) works correctly. It fills in data with views that are 100% in the SharedPrefernces file.

The second for (Map.Entry entry: usersPresets.getAll (). EntrySet ()) fills the view with data that may not exist in the specified file. There is - fills, no - goes further. And here is the problem.

public void CreateView(File[] loadFiles) { if (presetsStorage.exists()) { for (File loadFile : loadFiles) { //noinspection SpellCheckingInspection if (!loadFile.getName().contains("userpreset_")) { //noinspection UnnecessaryContinue continue; } else { if (loadFile.isFile()) { String presetsName = loadFile.getName(); presetsName = presetsName.substring(0, presetsName.length() - 4); usersPresets = getSharedPreferences(presetsName, MODE_PRIVATE); presetsContainer = (LinearLayout) findViewById(R.id.presetsContainer); View userPresetsView = getLayoutInflater() .inflate(R.layout.preset_fragment, presetsContainer, false); userPresetsView.findViewById(R.id.presetFragMenubtn) .setOnClickListener(viewClickListener); ((CircularSeekBar) userPresetsView.findViewById(R.id.presetFragA)). setProgress(usersPresets.getInt("a", 0)); ((CircularSeekBar) userPresetsView.findViewById(R.id.presetFragB)). setProgress(usersPresets.getInt("b", 0)); ((CircularSeekBar) userPresetsView.findViewById(R.id.presetFragC)). setProgress(usersPresets.getInt("c", 0)); TextView presetFragName = (TextView) userPresetsView .findViewById(R.id.userPresetName); presetFragName.setText(presetsName); presetFragName.setOnClickListener(viewClickListener2); presetsContainer.addView(userPresetsView); presetFragName.setText(presetsName.substring(11)); //Проблема в этом форе for (Map.Entry<String, ?> entry : usersPresets.getAll().entrySet()) { if (arr.contains(entry.getKey())) { // noinspection UnnecessaryContinue continue; } else { CardView flavor_cardView = (CardView) findViewById(R.id.flavor_cardView); final View usersFlavorsPreset = getLayoutInflater() .inflate(R.layout.preset_userflavor_fragment, flavor_cardView, false); ((TextView) usersFlavorsPreset.findViewById(R.id.tvUserFlavorPresetName)) .setText(entry.getKey()); ((TextView) usersFlavorsPreset.findViewById(R.id.tvUserFlavorPresetValue)). setText(String.valueOf(entry.getValue())); flavor_cardView.addView(usersFlavorsPreset); } } } } } } } 

I need to create as many new lines (View) as there are necessary data for this in the SharedPrefernces file. Now it works, but it creates views only on one line and with each iteration a handicap rewrites the values.

Screenshots of problems that can help to understand the essence (but this is not accurate) - https://drive.google.com/open?id=0Bx-Mv3rgZ3FyQWxWVzN2U294UVU

Prompt, please, where it is necessary to put for what values ​​would not be rewritten in the same place. I hope, explained a little lucidly. If it's still not clear, I will try again.

UPDATE : Added to CardView LinearLayout, and all elements from two files are now displayed in one place. So now another question - what needs to be changed, so that the placement would be correct?

 for (Map.Entry<String, ?> entry : usersPresets.getAll().entrySet()) { if (arr.contains(entry.getKey())) { // noinspection UnnecessaryContinue continue; } else { LinearLayout flavor_linear = (LinearLayout) findViewById(R.id.flavor_linear); final View usersFlavorsPreset = getLayoutInflater(). inflate(R.layout.preset_userflavor_fragment, flavor_linear, false); ((TextView) usersFlavorsPreset. findViewById(R.id.tvUserFlavorPresetName)).setText(entry.getKey()); ((TextView) usersFlavorsPreset. findViewById(R.id.tvUserFlavorPresetValue)).setText(String.valueOf(entry.getValue())); flavor_linear.addView(usersFlavorsPreset); } } 

enter image description here

  • It's not entirely clear what you want to do with the data so that each value from the usersPresets array usersPresets put in a separate CardView ? - Iman
  • Placing what? How do you want? - Flippy
  • @Iman here is the screen - i.stack.imgur.com/2ht8R.jpg . The green blocks are the created View in the first hand. Those. There are two files in the SharedPreferences repository: "test" and "stack". The same first for fills in data fields that are hidden under the red frame, but it does not matter, everything works as it should. Under the first green block "test" there are 4 lines: "from test", etc. So here. "from test" was taken from the test.xml file. The same situation with the lines "from stack". - Haze
  • I need that the data from each file is located in its block. In "test": "from test" and "from test 2", and in "stack" were "from stack" and "from stack 3". Now, these data from two different shared files are molded into the card of the first block. I hope you understand) - Haze
  • @Haze you have these stack tests, etc. Will there be an unknown quantity or a known? Those. the number of blocks is known how much or not? - Iman

2 answers 2

Try setting a condition in a loop:

 for (Map.Entry<String, ?> entry : usersPresets.getAll().entrySet()) { if (arr.contains(entry.getKey())) { // noinspection UnnecessaryContinue continue; } else { if (entry.getKey().equals(presetsName) { CardView flavor_cardView = (CardView) findViewById(R.id.flavor_cardView); final View usersFlavorsPreset = getLayoutInflater() .inflate(R.layout.preset_userflavor_fragment, flavor_cardView, false); ((TextView) usersFlavorsPreset.findViewById(R.id.tvUserFlavorPresetName)) .setText(entry.getKey()); ((TextView) usersFlavorsPreset.findViewById(R.id.tvUserFlavorPresetValue)) .setText(String.valueOf(entry.getValue())); flavor_cardView.addView(usersFlavorsPreset); } } } 
  • Did not help. The usersPresets.getAll (). EntrySet () does not contain a name. It is taken from the very name of the .xml file. But I understand what you mean. I changed it to this option: if (presetsName.substring (11) .equals (presetFragName.getText (). ToString ())), but the result is still the same. - Haze

The solution to the problem turned out to be elementary.

Changed this line:

 LinearLayout flavor_linear = (LinearLayout) findViewById(R.id.flavor_linear); 

On this:

 LinearLayout flavor_linear = (LinearLayout) userPresetsView.findViewById(R.id.flavor_linear); 

Now everything is where it should be.