How to combine sets of images, strings and integers in an easy-to-use format

So, I'm trying to find a better way to combine many data types together. Inside my code, I create a class. Outwardly, I want to have a single place to manage the following data types, or at least pointers to the specified data. Note that there are many sets of this data, and I expect more to be added over time.

EDIT: I greatly simplify what was here.

Image that at compile time I have a list of objects. Each object has some characteristic that will have all the objects. These include:

  • ImageImage
  • Some lines
  • Array of integers
  • Array of strings

I want to find a way to store all this together outside of any code. Ideally, I would like to use XML, with something that vaguely looks like this:

<storage_type> <image resid="@id/image1" /> <name>Item_Name1</name> </storage_type> <storage_type> <image resid="@id/image2" /> <name>Item_Name2</name> </storage_type> 

Images are currently stored as variable resources. I just don't know how I can put an image link in an XML file.

I am not married to the idea of ​​XML files if you can find a better solution. All I want is the only database, a way to access this information.

This data should not be changed unless I submit a new update. They do not need to be updated at runtime, but I would definitely like to update them between assemblies, from time to time. I have heard about settings that may work, but I'm not sure if they can be updated by the user in any way.

+4
source share
3 answers

I was able to find a solution with significant help on this issue .

  • My data is stored in an XML array.
  • This data contains an array index that points to an array of images supported under a normal resource.

My data storage file looks like this:

 <storage_type> <image imageArrayIndex="1" /> <name>Item_Name1</name> </storage_type> <storage_type> <image imageArrayIndex="2" /> <name>Item_Name2</name> </storage_type> 

I have a file in my res / values ​​folder that looks like this:

 <resources> <string-array name="images"> <item>@drawable/default</item> <item>@drawable/image1</item> <item>@drawable/image2</item> </string-array> </resources> 

And somewhere deep in parsing the XML I have:

 TypedArray imgs = context.getResources().obtainTypedArray(R.array.achievement_images); //Whole bunch of code to parse XML, eventually coming to this line: else if (name.equals("image")) { imageRes=imgs.getResourceId(xpp.getAttributeIntValue(null,"imageArrayIndex", 0),-1); } 

Alternatively, I could just save the image name as shown below:

 <storage_type> <image resid="image1" /> <name>Item_Name1</name> </storage_type> <storage_type> <image resid="image2" /> <name>Item_Name2</name> </storage_type> 

And then get it by parsing the XML tag and getting the resource identifier:

 int resid=Context.getResources().getIdentifier (name_string_Image, "drawable", null); 
+2
source

SharedPreferences is an option, but later on, even if you just need to change the values, you will have to make changes to your code. On the other hand, the xml path is more flexible.

In the future, if you just want to change the parameter values, change the xml file and click it on the phones of your users. No need to change code, go through testing cycles, etc. And in case you want to add / remove material, in any case, you will need to change the code (especially the adding part)

The overhead for the xml course will be part of the reader / author.

+1
source

I think you could create a custom object that stores these values, and has a way to output it to a JSON string (for saving in Prefs) and reconstruct it by taking a JSON string. You can also use methods that return an image of an object. Here is an example of a class that I wrote:

 public class MyObject { private static final String IMAGE_RES_ID = "ImageResId"; private static final String STRING1 = "String1"; private static final String STRING2 = "String2"; private static final String INT_ARRAY = "IntArray"; private static final String STRING_ARRAY = "StringArray"; private int mImageResId; private String mString1, mString2; private int[] mIntArray; private String[] mStringArray; public MyObject(int imageResId, String s1, String s2, int[] intArray, String[] stringArray) { mImageResId = imageResId; mString1 = s1; mString2 = s2; mIntArray = intArray; mStringArray = stringArray; } public MyObject(String jsonString) throws JSONException { JSONObject jsonObject = new JSONObject(jsonString); // Get single values mImageResId = jsonObject.getInt(IMAGE_RES_ID); mString1 = jsonObject.getString(STRING1); mString2 = jsonObject.getString(STRING2); // Get arrays JSONArray intArray = jsonObject.getJSONArray(INT_ARRAY); mIntArray = new int[intArray.length()]; for (int i = 0; i < mIntArray.length; i++) mIntArray[i] = intArray.getInt(i); JSONArray stringArray = jsonObject.getJSONArray(STRING_ARRAY); mStringArray = new String[stringArray.length()]; for (int i = 0; i < mStringArray.length; i++) mStringArray[i] = stringArray.getString(i); } public int getImageResId() { return mImageResId; } public String getString1() { return mString1; } public String getString2() { return mString2; } public int[] getIntArray() { return mIntArray; } public String[] getStringArray() { return mStringArray; } public Drawable getImageDrawable(Context context) { return context.getResources().getDrawable(getImageResId()); } private JSONObject toJsonObject() throws JSONException{ JSONObject toReturn = new JSONObject(); // Put single values toReturn.put(IMAGE_RES_ID, getImageResId()); toReturn.put(STRING1, getString1()); toReturn.put(STRING2, getString2()); // Put arrays JSONArray intArray = new JSONArray(); for (int value : mIntArray) intArray.put(value); JSONArray stringArray = new JSONArray(); for (String value : mStringArray) stringArray.put(value); toReturn.put(INT_ARRAY, intArray); toReturn.put(STRING_ARRAY, stringArray); return toReturn; } public String toJsonString() throws JSONException { return toJsonObject().toString(); } } 

Create this if you have all the necessary information that you want to keep. Then save the result of toJsonString () in SharedPreferences and restore it if necessary at runtime.

+1
source

Source: https://habr.com/ru/post/1402047/


All Articles