Categories
discuss

How to clean completely select2 control?

I’m working with the awesome select2 control.

I’m trying to clean and disable the select2 with the content too so I do this:

$("#select2id").empty();
$("#select2id").select2("disable");

Ok, it works, but if i had a value selected all the items are removed, the control is disabled, but the selected value is still displayed. I want to clear all content so the placeholder would be showed. Here is a example I did where you can see the issue: http://jsfiddle.net/BSEXM/

HTML:

<select id="sel" data-placeholder="This is my placeholder">
    <option></option>
    <option value="a">hello</option>
    <option value="b">all</option>
    <option value="c">stack</option>
    <option value="c">overflow</option>
</select>
<br>
<button id="pres">Disable and clear</button>
<button id="ena">Enable</button>

Code:

$(document).ready(function () {
    $("#sel").select2();

    $("#pres").click(function () {
        $("#sel").empty();
        $("#sel").select2("disable");
    });

    $("#ena").click(function () {
        $("#sel").select2("enable");
    });
});

CSS:

#sel {
    margin: 20px;
}

Do you have any idea or advice to this?

Answer

Why all this trouble???

use:

 $('#sel').select2('data', null);
Categories
discuss

importScripts (web workers)

I have tried to use importScripts to load a second JavaScript file into my web worker, but although no error occurred, it didn’t work. I narrowed the problem down to this very simple situation:

In the main HTML file:

<script>
var w = new Worker("script1.js");
w.addEventListener("message", function(e){
    alert(e.data);
})
w.postMessage();
</script>

In script1.js:

self.addEventListener("message", function(e){
    var a = 5;
    importScripts("script2.js");
    self.postMessage(a);
})

In script2.js:

a = 6

I would like to see a dialog displaying 6, because a was changed from 5 to 6 by importing script2.js, but the dialog shows 5. What am I missing here?

Answer

Using var a in the function means that a will always be private. Since importScripts adds to the global scope, JS prefers to access the more localized a in the function that posts a. You can post self.a instead, which shall be 6, as you expected.

EDIT: Someone recently asked me about this in person, so I made a demo to clarify the behaviour: http://pagedemos.com/importscript/

Categories
discuss

Java: Use DecimalFormat to format doubles and integers but keep integers without a decimal separator

I’m trying to format some numbers in a Java program. The numbers will be both doubles and integers. When handling doubles, I want to keep only two decimal points but when handling integers I want the program to keep them unaffected. In other words:

Doubles – Input

14.0184849945

Doubles – Output

14.01

Integers – Input

13

Integers – Output

13 (not 13.00)

Is there a way to implement this in the same DecimalFormat instance? My code is the following, so far:

DecimalFormat df = new DecimalFormat("#,###,##0.00");
DecimalFormatSymbols otherSymbols = new   DecimalFormatSymbols(Locale.ENGLISH);
otherSymbols.setDecimalSeparator('.');
otherSymbols.setGroupingSeparator(',');
df.setDecimalFormatSymbols(otherSymbols);

Answer

You can just set the minimumFractionDigits to 0. Like this:

public class Test {

    public static void main(String[] args) {
        System.out.println(format(14.0184849945)); // prints '14.01'
        System.out.println(format(13)); // prints '13'
        System.out.println(format(3.5)); // prints '3.5'
        System.out.println(format(3.138136)); // prints '3.13'
    }

    public static String format(Number n) {
        NumberFormat format = DecimalFormat.getInstance();
        format.setRoundingMode(RoundingMode.FLOOR);
        format.setMinimumFractionDigits(0);
        format.setMaximumFractionDigits(2);
        return format.format(n);
    }

}
Categories
discuss

Samsung hovering API with finger

Samsung has done something great with the hovering API, and I’m interested in including it in my app. The problem is that the hovering API included in the SPen SDK only work with the SPen and not with the finger detection like the latest apps do on the Samsung Galaxy S4.

Can anyone provide me with a link to the right SDK or even a sample project?

EDIT: Nifhel commented, fallow this link: Floating Touch on Galaxy S4 (requires API 12 and to create a new class that inherit from TextView for instance)

EDIT: The accepted answer is a better solution and works perfectly well (requires API 14 but easier to use).

Answer

I’ve tried out the method described on the link recommended by Nifhel. The only thing you have to do is adding a new intent filter for your Activity in the manifest, no need to override “dispatchGenericMotionEvent”:

<intent-filter>
    <action android:name="com.sec.android.airview.HOVER" />
</intent-filter>

After that you have to add an onHoverListener to you View and it will work fine. For example in my ListAdapter:

convertView.setOnHoverListener(new OnHoverListener() {
    @Override
    public boolean onHover(View v, MotionEvent event) {
        Log.d("ListAdapter", "Hover: " + item);
        return false;
    }
});
Categories
discuss

How to find out that given String is already in Java String pool?

Is there any method or technique how to know that given String s is already in the String pool? How and when is Java String pool creating? What does initial members it contain?

Answer

My answer is: there is no general solution to that. What you can do is:

boolean wasAlreadyInterned = str.intern() == str;

but this has the side-effect, that now it is interned for sure.

JavaDoc of String#intern says, that the class String privately maintains a pool of strings, that is initially empty.

If you look at the implementation of the class String all you see is

public native String intern();

The Java Language Specification, Chapter 3.10.5, String literals says:

string literals – or, more generally, strings that are the values of constant expressions (§15.28) – are “interned” so as to share unique instances, using the method String.intern.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..