Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I was looking for how to detect if an Android Q device has Dark Mode enabled, and the only result I found was in Kotlin:
fun isDarkTheme(activity: Activity): Boolean {
return activity.resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}
IF it is correct, then can I please have it in Java? :)
Here is the java Version
private Boolean isDarkTheme(Activity activity) {
return (activity.getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
Java getResources() ->Kotlin resources
Java getConfiguration() ->Kotlin configuration
Java & ->Kotlin and
as you see it's simple Setters and Getters in koltin are accessed by property name
I realised there might be a little more going on here to someone who's completely unfamiliar with Kotlin, so just in case:
boolean isDarkTheme(Activity activity) {
return activity.getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}
all the get calls get converted to property syntax in Kotlin, so you can just access them like fields. The rest of the stuff just moves around a bit
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to use this code in .net Framework 2.0 but the problem is that labmdas(WHERE) can't be used in this version, is there a way?
var serializer = new JavaScriptSerializer();
retorno = serializer.Deserialize<RespuestaCvt>(respuesta);
var soloServidores = retorno.Rows.Where(x => listaEstados.Contains(x.EstadoId)).Where(x => listaTipos.Contains(x.TipoId)).ToList();
return soloServidores;
Since Lambda expressions were not available until C# 3 you will have to change the way you do the actual row filtering. Since you're returning List<> we can just go back to the pre-LINQ way of doing things:
var soloServidores = new List<TheRowType>();
foreach (var row in retorno.Rows)
{
if (listaEstados.Contains(row.EstadoId) && listaTipos.Contains(x.TopiId))
soloServidores.Add(row);
}
return soloServidores;
Of course you could do it the hard way and extract the lambda to a static method then implement a suitable Where(...) extension... but if you're going to try to emulate C# features from a later language version then you're usually better off migrating your project to that later version.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Wasn't sure how to search for this and was unable to find the answer to set the following to true (Java):
boolean flagRefund = true;
if (flagRefund){
// Suppose to set wasRefunded field for user in DB to true or 1
dbModelUser.getWasRefunded();
}
This is a lesson to teach you how to read the code if you see the method that starts with "get" then you use it to get some value from the method that returns it. When the method starts with "set" then this method usually returns void and takes parameters. Like this
boolean flagRefund = true;
if (flagRefund){
// Suppose to set wasRefunded field for user in DB to true or 1
dbModelUser.setWasRefunded(flagRefund);
}
Usually, get methods are used to retrieve information, not to set information. Look if there is some set in your code method to achieve it.
Also, you don't pass any value to your method getWasRefunded as parameter so it is impossible that it could set any info.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm doing a conversion from a COBOL copybook to a SWIFT message. First I'm parsing the copybook with a copybookHelper class and then I write a giant set of if statements to test all the fields and populate my SWIFT string. Is there a less cumbersome way to implement it (without the gigantic set of ifs)?
SWIFT = Society for Worldwide Interbank Financial Telecommunication
Would an Enum of the different message types help? With an enumeration in place, parsing a particular type and populating the swift string could be as simple as:
SwiftMessage msg = Enum.valueOf(SwiftMessage.class, "MT001");
msg.populateString(/* parameters? */);
An example of how you would write the enum implementation:
enum SwiftMessage {
MT001,
MT002 { void populateString() { /* override implementation */ } },
...
void populateString() { /* default implementation */ }
}
It's hard to be more specific without any details of what you are trying to do.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Hi I want to print the SingleLinkedList in reverse.
static void Collections.reverse(List list):
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html
See Collections.reverse (JavaDoc)
I don't know for "The SingleLinkedList" class (is it even a List implementation ?) but if it works as its name states, the best way (more effective one) would be to go through the data structure and put every element in a stack. Then simply read the stack.
public print(Element el) {
if (el.next!=null)
print(el.next);
System.out.print(el.value+" ");
}
Basicaly its puting it in (call) stack an reading it (as someone else sugested). Nonrecursive version using explicit stack is usualy faster, does not risk StackOverflowException and takes longer to write.