Categories
discuss

Custom AlertDialog – problem with android.R.id.custom

I’m sure I’m missing the point here so I’m hoping someone can explain.

I want to create a popup when a user touches an ImageView. I had a look at AlertDialog and the docs say…

If you want to display a more complex view, look up the FrameLayout called “custom” and add your view to it:

…with the following code…

    FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
    fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

So as a test I tried the following in my onCLick() method…

    TextView tv = new TextView(this);
    tv.setText("Hello World");
    FrameLayout customFrameLayout = (FrameLayout) findViewById(android.R.id.custom);
    customFrameLayout.addView(tv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

The last line of the above where I’m calling addView throws a NullPointerException which makes me think there’s a problem with android.R.id.custom.

So the question is, what is wrong with the above and also is there a better way of creating a custom popup (perhaps by using the Dialog class or extending it)?

NOTE: I’m only using TextView in this example as a test, I want to add something more complex for my actual popup.

Answer

One option is to create an Activity and style it using the dialog theme:

<activity android:theme="@android:style/Theme.Dialog">

See applying themes for more information.

Categories
discuss

JQGrid: Dynamically set a cell to uneditable based on content

I’m having some issues getting some cells (with cellEdit: true) to be non-editable even though the column is set to editable.

I’ve tried many ways, like beforeEditCell, formatters, etc. None seem to work.

The closest I’ve got is by setting a formatter to the column that I’d like to be editable and then using setCell to set the ‘not-editable-cell’ class (snippet below). The first time you click the cell, it unfortunately goes into edit mode, but if you click elsewhere and try to re-edit the cell, it’s successfully uneditable.

I’ve also tried using the same snipped but inside of beforeEditCell, it successfully stops the cell from being edited but in turn ‘freezes’ the grid. You can no longer select any other cell.

function noEditFormatter(cellValue, options, rowObject) {
    if (cellValue == 'test')
        jQuery("#grid").jqGrid('setCell', options.rowId, 'ColName', '', 'not-editable-cell');
    return cellValue;
}

Any help would be much appreciated.

Answer

The idea to use setCell method to add class ‘not-editable-cell’ to the cells which should be not-editable is correct. You choose only the wrong place to do this. Inside of custom formatter, the grid can be not yet built till the end. I recommend you to use loadComplete or gridComplete to examine the grid contain of the current page and mark some cells as not-editable.

I prepared an example which demonstrate this. Like in your example all cells having “test” text are marked as non-editable. In the way you can examine one cells and mark another cells as non-editable.

Categories
discuss

How to get string.format to complain at compile time

The compiler has access to the format string AND the required types and parameters. So I assume there would be some way to indicate missing parameters for the varargs … even if only for a subset of cases. Is there someway for eclipse or another ide to indicate that the varargs passed might cause a problem at runtime ?

Answer

It looks as if FindBugs can solve your problem. There are some warning categories related to format strings.

Categories
discuss

Mutable objects and hashCode

Have the following class:

public class Member {
private int x;
private long y;
private double d;

public Member(int x, long y, double d) {
    this.x = x;
    this.y = y;
    this.d = d;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + x;
    result = (int) (prime * result + y);
    result = (int) (prime * result + Double.doubleToLongBits(d));
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj instanceof Member) {
        Member other = (Member) obj;
        return other.x == x && other.y == y
                && Double.compare(d, other.d) == 0;
    }
    return false;
}

public static void main(String[] args) {
    Set<Member> test = new HashSet<Member>();
    Member b = new Member(1, 2, 3);
    test.add(b);
    System.out.println(b.hashCode());
    b.x = 0;
    System.out.println(b.hashCode());
    Member first = test.iterator().next();
    System.out.println(test.contains(first));
    System.out.println(b.equals(first));
           System.out.println(test.add(first));

}

}

It produces the following results:
30814 29853 false true true

Because the hashCode depends of the state of the object it can no longer by retrieved properly, so the check for containment fails. The HashSet in no longer working properly. A solution would be to make Member immutable, but is that the only solution? Should all classes added to HashSets be immutable? Is there any other way to handle the situation?

Regards.

Answer

Objects in hashsets should either be immutable, or you need to exercise discipline in not changing them after they’ve been used in a hashset (or hashmap).

In practice I’ve rarely found this to be a problem – I rarely find myself needing to use complex objects as keys or set elements, and when I do it’s usually not a problem just not to mutate them. Of course if you’ve exposed the references to other code by this time, it can become harder.

Categories
discuss

geospatial queries in javascript [closed]

I’m looking for a library in javascript that would allow me to make geospatial queries. I know about OpenLayers and GoogleMaps, but this two do not support things like union intersection and so on.

+----+
|    |
|  +-+--+
+--+-+  |
   |    |
   +----+

to

    +----+
    |    |
    |    +--+
    +--+    |
       |    |
       +----+

Answer

Update 2014-04-29: Check out Turf, looks really promising

JSTS can do unions of geometric objects in the browser. JSTS integrates with the openlayers library and it extends openlayers geometric classes (e.g. OpenLayers.Geometry.Polygon) so they are capable of geometric operations. Example:

>> var poly1 = OpenLayers.Geometry.Polygon.createRegularPolygon(
                                   new OpenLayers.Geometry.Point(0.0,0.0), 2, 5);
>> var poly2 = OpenLayers.Geometry.Polygon.createRegularPolygon(
                                   new OpenLayers.Geometry.Point(1.0,1.0), 2, 4);
>> var poly_u = poly1.union(poly2);
>> var poly_d = poly1.difference(poly2);
>> print(poly_u);
POLYGON((1.5667154718422638 -0.4142135623730949,1.1755705045849463 -1.618033988749895,
  -1.1755705045849465 -1.6180339887498947,-1.902113032590307 0.618033988749895,
  -0.41421356237309503 1.6990562312593451,-0.4142135623730949 2.414213562373095,
   2.414213562373095 2.414213562373095,2.414213562373095 -0.4142135623730949,
   1.5667154718422638 -0.4142135623730949))

Geoscript JS is nice if you want to do serverside geometric operations in JS.

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..