1

I am quite new to Java and am currently coding an Android application. In my "Shortcuts" class, I have this bit of code (more of course, but not useful to you, I don't think):

final String[] items = new String[]{ "Please select a category",scanner.getCategorys() };
        @SuppressWarnings({ "unchecked", "rawtypes" })
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        selection.setAdapter(adapter);

I have another class named "Scanner", which has this code:

String categorys = "test";

public String getCategorys() {
    return categorys;
}

This does work, and in my Spinner, I can fill in my "Please select a category" option with a single value (i.e "test"). The problem is, I would like to be able to select multiple categories. If I do this in "Shortcuts" class:

final String[] items = new String[]{ "Please select a category","test","test2" };

It would work, but I would like to set it from the "Scanner" class, so I tried this:

String[] categorys = "test","test2";

public String[] getCategorys() {
    return categorys;
}

But it just gives me the error:

String cannot be converted to String[]

I would be grateful for any help.

4 Answers 4

2

This is wrong:

String[] categorys = "test","test2";

Change it to

String[] categorys = {"test","test2"};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that removed some errors, but I still get errors where I put scanner.getCategorys(). I've edited the post.
Yes it will. Just place String[] categorys {"Please select a category", "test","test2"}. Then initialize your items like that: String[] items = scanner.getCategorys()
1

In your last code sample, you have to put { } around your strings, like so:

String[] categorys = { "test","test2" };

public String[] getCategorys() {
    return categorys;
}

According to your edit above, you can't add a String[] to an existing String[]. You need to add every item in the String[] you get from getCategorys() to the other array. I would probably switch to an ArrayList<string> in this case, so you don't have to decide what size the array should be, then add each item.

1 Comment

Thanks, that removed some errors, but I still get errors where I put scanner.getCategorys()
1

You can initialize categorys as follows

String[] categorys = {"test","test2"};

I'd call the variable as 'categories'.

EDIT : scanner.getCategorys() cannot be used as the constructor expects a String and not a string array. A good idea would be use ArrayList as pointed out by uncocoder. You could then just use the add method to include "Please select a category".

1 Comment

Thanks, that removed some errors, but I still get errors where I put scanner.getCategorys(). I've edited the post.
0

I recomment to use ArrayList, it's fast and easy like this

ArrayList<String> strings = new ArrayList<String>();
strings.add("SOME TEXT");
strings.get(0);
//and more

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.