Question

I have xml layout in values/menu_items.xml for menu items:

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

<string-array name="menu_items">
    <item ></item>
    <item ></item>
    <item ></item>
    <item ></item>
</string-array>

I populate these items from my database with following code from MainActivity.java class:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    mLayout = (MainLayout) this.getLayoutInflater().inflate(
            R.layout.activity_main, null);
    setContentView(mLayout);


    //opens database
    datasource = new CommentsDataSource(this);
    datasource.open();



    //menu_items list
    lvMenuItems =  getResources().getStringArray(R.array.menu_items);

    ArrayList<Category> meniVrednosti;
    meniVrednosti = new ArrayList<Category>();

//returns number of requested elements:
    int i= datasource.vratiBrojElemenataSaZadatimId(0);


    //for loop to populate menu_items.xml between tags <item></item>:
    for(int j=0;j<i;j++)
    {
        lvMenuItems[j] = (datasource.getCategoryViaParentIDandID(0, 
                10+j).toString()).replaceAll("\\[", "").replaceAll("\\]","");

    }
...

So, my question is how i can put images (picture, logo etc..) together with that names from database. For Example:

  • (This is where picture goes) (this is MenuItemNO1, text between tags are from database)
  • (This is where picture goes) MenuItemNO2
  • (This is where picture goes) MenuItemNO3

I tried so many ways to do it, but i couldn't. Nothing worked for me far by now. I hope so that you can help me. Tell me if u need more info. Thanks in advance. Regards.

Was it helpful?

Solution

You can only put data that can be represented as a String in a string-array element. However, if you have severals pieces of data you want in each item and they can all be represented as a String, you can then separate them with a delimiter. Generally I use the pipe symbol | then you can get the array out of the resources, iterate through them and split on your delimiter to get the separate data again.

Updated with example:

Assuming some XML like this:

<string-array name="menu_items">
    <item>/path/to/images/image1.png|First Image</item>
    <item>/path/to/images/image2.png|Second Image</item>
    <item>/path/to/images/image3.png|Third Image</item>
    <item>/path/to/images/image4.png|Fourth Image</item>
</string-array>

You could parse it to get the image and title out like this:

private Map<String, String> parseImageArray {
    Map<String, String> imageMap = new Map<String, String>();
    String[] imageArray = getResources().getStringArray(R.array.menu_items);

    for (String arrayEntry : imageArray) {
        String[] keyValue = arrayEntry.split("|");
        imageMap.put(keyValue[1], keyValue[0]);
    }

    return imageMap;
}

Now you have a Map of the image title to image paths

OTHER TIPS

You can put strings only in the item-elments. You can put in the path of your images, name or the size or whatever as a string but you can't put any other objects as far as I know and as the name "string-array" may suggest :). I suppose, what you need is a custom menu, so you can style the menu items as you wish.

Check this link to get an introduction http://developer.android.com/guide/topics/ui/menus.html . Search for some custom menu on android tutorials on the internet. Right now I can't recommend you any, sorry. But maybe someone else can give you the right direction. BTW What menu do you like to create - options menu?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top