Categories
discuss

Count the lines of a PRE element using jQuery

How can I count how many lines of text a <pre> tag contains?

I want to append a absolute div with line numbers next to it.

Answer

you could use the javascript split function to count the line breaks.

$('pre').html().split(/n/).length

better

$('pre').html().match(/n/)
Categories
discuss

Does an AlarmManager alarm persist if app is killed by system due to low memory?

I already know that if your app is forced closed by an app killer, or through the Android settings, your pending alarms are lost. But what if Android itself killed it due to very low memory. Would your alarms be lost in this case? How often does this actually happen?

I see two ways to recover from losing an alarm:

  1. If your app has a main activity, use onCreate() to check if your alarm is running behind schedule (alarm should store the time it last ran in a pref), and reschedule it as necessary.

  2. Find some common recurring Android task to latch onto with a receiver to do the same check as above.

Both of these have some obvious major cons. Any other ideas?

Answer

I already know that if your app is forced closed by an app killer, or through the Android settings, your pending alarms are lost.

Definitely a task killer on Android 2.1 and earlier has this effect. I am not aware that using the Settings app has this effect, though I have not tried it.

Would your alarms be lost in this case?

No.

Categories
discuss

jQuery $.ajax() executed twice?

Here is a button:

<input type="button" value="add to cart" id="addToCart" />

and a bound event:

$("#addToCart").bind('click',function(){
                $.ajax({
                    url: '/cartManager/add',
                    data:{
                        pictureId: currentImageId,
                        printSize: $("#size option:selected").val(),
                        paperType: $("#paperType option:selected").val(),
                        quantity: 1
                    },
                    success: function(){
                        $("#modal").html("<h1>ОК</h1><p>Closing in a sec</p>").delay(1000);
                        $("#modal").overlay().close();

                    }
                });
            return false;
            });

And everything works find apart one thing that kind of bothers, I see two requests in Chrome dev console for this:

  1. add /cartManager:
Request URL:http://127.0.0.1:8000/cartManager/add?pictureId=4&printSize=2&paperType=1&quantity=1
Request Method:GET
Status Code:301 MOVED PERMANENTLY
  1. add /cartManager/add?:
Request URL:http://127.0.0.1:8000/cartManager/add/?pictureId=4&printSize=2&paperType=1&quantity=1
Request Method:GET
Status Code:201 CREATED

Request headers for both are pretty much the same, the only difference in request headers:

first is cartManager/add?pictureId= and so on and the second one is cartManager/add/?pictureId – the ‘/’ after /add

Is there something wrong with my javascript?

Answer

There’s nothing wrong per-se, but you should add the trailing slash to /cartManager/add yourself.

What’s happening is that the web server is sending a 301 redirect to the AJAX client with a new URL, so it issues a new request to the proper URL (i.e. with the trailing slash).

Categories
discuss

Android Custom Dialog

I’m trying to make a custom dialog, following the tutorial on the Android developer site, but it crashes every time I try to show the dialog. Here’s my code:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();

And here’s my XML for the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/btnConfirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add text"
        android:layout_below="@+id/txtNewText"
        android:layout_alignParentLeft="true">
    </Button>
    <EditText
        android:id="@+id/txtNewText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </EditText>
</RelativeLayout>

Answer

Consider the pattern:

private static final int MY_DIALOG= 0;

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
        case MY_DIALOG:
            dialog= getInstanceMyDialog();
            break;
        default:
            dialog = null;
    }
    return dialog;
}

private Dialog getInstanceMyDialog() {
    final Dialog d= new Dialog(this); //<=====THIS
    d.setContentView(R.layout.custom_dialog);
    d.setTitle("Custom Dialog");
    return d;
}

JAL

Categories
discuss

Alternative views instead of Stackviews for android versions lower to android 3.0

I am trying to implement StackView behaviour in my application but I believe this widget is only available from android 3.0 onwards. I am an intermediate developer for android OS and hence have limited knowledge on customized views.

Could some one please provide some pointers as to how i can achieve Stackview in android 2.2/2.3?

Answer

There is no component in the SDK with this exact functionality prior to API Level 11 (3.0), so you will need to create your own custom View to get the desired effect.

The easiest/quickest way would be to create a custom view that extends FrameLayout since children of a FrameLayout automatically stack on top of each other and your custom view will inherit this behaviour.
You’ll also want to override the way children are laid out so that you adjust the x and y position of the children depending on where they are in the stack.

You will have to make your own methods to go to the next/previous view – you’d have to do this by re-ordering the the child views (remove them all, then add them back in the new order).

The solution I’ve described doesn’t include the view recycling you get with AdapterViews, so wouldn’t be suitable for a large number of stacked items.

Alternatively, find the StackView source code and copy it into your project, though this will probably also involve copying a large amount of other Android classes into your project, since StackView will undoubtedly access private members of super-classes that are only accessible from the widgets package (without Reflection).

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