I have my variable the first class like:
MyClass{
companion Object{
@JvmField val foo = mapOf("a" to "b")
}
}
And when I call it from another class like:
setBackgroundColor(Color.parseColor(MyClass.foo["..."]));
appears an error saying "Array type expected; found: 'java.util.map < java.lang.String,java.lang.String>'" What is the problem?
Note that if I do the same call in MyClass it works perfectly
This is my real array values:
companion object{
@JvmField val darkMode = mapOf(
"bgColor" to "#000000",
"cardColor" to "#262626"
)
}
The error comes when I call it from a class that extends RecyclerView.ViewHolder
I just tried this:
val foo = mapOf("a" to "Red")
someView.setBackgroundColor(Color.parseColor(foo["a"]))
And it works fine, can you share more details about the exception?
I tried using your MyClass
exactly as written, except i've replaced the value of a
by Red
instead of your b
string.
Are you using parseColor
correctly?
/**
* </p>Parse the color string, and return the corresponding color-int.
* If the string cannot be parsed, throws an IllegalArgumentException
* exception. Supported formats are:</p>
*
* <ul>
* <li><code>#RRGGBB</code></li>
* <li><code>#AARRGGBB</code></li>
* </ul>
*
* <p>The following names are also accepted: <code>red</code>, <code>blue</code>,
* <code>green</code>, <code>black</code>, <code>white</code>, <code>gray</code>,
* <code>cyan</code>, <code>magenta</code>, <code>yellow</code>, <code>lightgray</code>,
* <code>darkgray</code>, <code>grey</code>, <code>lightgrey</code>, <code>darkgrey</code>,
* <code>aqua</code>, <code>fuchsia</code>, <code>lime</code>, <code>maroon</code>,
* <code>navy</code>, <code>olive</code>, <code>purple</code>, <code>silver</code>,
* and <code>teal</code>.</p>
*/
@ColorInt
public static int parseColor(@Size(min=1) String colorString) {
if (colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color");
}
return (int)color;
} else {
Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));
if (color != null) {
return color;
}
}
throw new IllegalArgumentException("Unknown color");
}
Ok, you said "a class that extends RecyclerView.ViewHolder
" but said class is just an abstract class inside the RecyclerView class, so it has no setBackgroundColor
in it, unless you're doing something like:
yourViewHolderInstance.itemView.setBackgroundColor
.
So what is the signature of your setBackgroundColor?
Does it look like public void setBackgroundColor(@ColorInt int color) {
?
I just added this to my app just for fun:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as BaseViewHolder).bind(getItem(position))
// For Filippo :)
holder.itemView.setBackgroundColor(Color.parseColor(MyClass.foo["a"]))
}
And well... it all looks very RED now. :)
Ok, so according to your last update, you're calling this from Java, therefore you cannot use the Kotlin syntax...
Do this:
final Map<String, String> foo = MyClass.foo;
yourView.setBackgroundColor(Color.parseColor(foo.get("a")));
Obviously, you can avoid the intermediate assignment and go for:
v.setBackgroundColor(Color.parseColor(MyClass.foo.get("a")));
I usually prefer the former, especially if you give it all meaningful names and need debugging, but there's no real difference as far as I'm concerned.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.