Categories
discuss

Modify underlying form.reset() values

I’m using $('form-selector').get(0).reset() to reset form values to their original page load state.

After editing, the form will submit via $.ajax() and I’ll have new “default” values on our server. The form element will still exist in the dom, and the user can submit again to update. I’d like the “default” (reset values) to reflect what’s on our server (ignoring any other external updates). Is it possible to update the underlying values that form.reset() will change each form element to without a page refresh?

Cross-browser support would be nice, but since this is an internal app, Google Chrome only is sufficient.

HTML

<form>
    <input type="text" value="foo" name="bar" />
    <input type="submit" value="Submit" />
    <Input type="reset" value="Reset" />
</form>

JAVASCRIPT

$(function(){
    $('form').submit(function() {
        // Omitting code that sends form values to the server

        // TODO: update underlying form.reset()
        // values to what's currently in each
        // form element.

        return false;
    });
});

UPDATE

Ack! I failed to mention that I’m looking for something to handle all form element types. i.e. input[type=text], input[type=radio], input[type=checkbox], select, textarea.

Would be especially awesome if it can handle HTML5 form elements as well… i.e. input[type=date], input[type=number], input[email], input[url], input[type=range], input[type=color], etc.

Sorry for the confusion.

Answer

If you change attributes of the form elements directly, rather than using the .val() method, the new values will be reflected on a form reset. You’ll need to treat text fields differently from radio buttons, checkboxes, etc.

    $('input').attr('value', function() { return this.value });
    $('textarea').prop('innerHTML', function() { return this.value });                                              
    $(':checked').attr('checked', 'checked');                                              
    $(':selected').attr('selected', 'selected');
    $(':not(:checked)').removeAttr('checked');
    $(':not(:selected)').removeAttr('selected');                                              
Categories
discuss

Unexpected top and bottom padding on a TextView?

I have a TextView. I want to set its background color. But the color fills more top space than it does bottom space.

<!-- red background. -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textStyle="bold"
    android:background="#f00"
    android:text="10.0" />

This is how it renders (zoomed in to demonstrate, green lines added by me):

enter image description here

So the extra space up top makes it look like the text is not centered within its own container. Is this expected? I’d like the text to appear vertically centered within its own container (the red area).

This is on a galaxy nexus, stock version of jellybean.

Thanks

Answer

Add the following attribute:

android:includeFontPadding="false"
Categories
discuss

Convert from Class to TypeReference

Basically just the opposite of this question. I want to convert a Class<T> object to a TypeReference<T> object.

I have tried:

foo(Class<T> valueType)
{
   TypeReference ref = new TypeReference<T>(){}; 
}

but that just returns a type reference of the classes’s super class. I also tried:

foo(Class<T> valueType)
{
   TypeReference ref = new TypeReference<valueType>(){}; 
}

and

foo(Class<T> valueType)
{
   TypeReference ref = new TypeReference<valueType.getRawClass()>(){}; 
}

But the second two don’t compile. How do I do this?

Answer

You can override getType() to return your type:

new TypeReference<T>(){
    @Override
    public Type getType() {
        return valueType;
    }
}; 
Categories
discuss

“&” or “,”: What is the difference between A and A

Multiple generic interface separator: “,” or “&”

I was always using A<T extends I1, I2> but today, I saw A<T extends I1 & I2>. What is the difference between these two notation?

Does it have a different meaning? Purpose?

Answer

A<T extends I1, I2>

is a type A with two parameters

  1. T which must be a subtype of I1
  2. I2

You can read this as “A <preposition> type T which extends I1 <preposition> type I2”, so SortedMap<Key extends comparable, Value> is a “map from comparable type key to type value”


A<T extends I1 & I2>

is a type A with a single parameter

  1. T which must be a subtype of I1 and a subtype of I2

You can read this as “A <preposition> type T which extends I1 and I2” so a SortedSerializableSet<T extends Serializable & Comparable> can be read “a sorted set of elements of type T which is both serializable and comparable”

Categories
discuss

Android 4.1.2 dialogs are called twice

I have this issue with my app when I call to show a dialog, it is being called twice somehow. This only happens with android 4.1 and above. Lower version works fine so I dont think it’s any code issue.

Did you heardencountered this issue before?

here the code:

Button edit = (Button) ad.findViewById(R.id.editBtn);
        edit.setTypeface(roboto);
        edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                setDate();
                ad.dismiss();
            }
        });

        ad.show();

        ad.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                shiftsActivity.setPressed(true);

            }
        });
    }

    public void setDate() {
    // Initialize and open the set date dialog
    DatePickerDialog setDateDialog = new DatePickerDialog(Shifts.this,
            datePickerListener, dateAndTime.get(Calendar.YEAR),
            dateAndTime.get(Calendar.MONTH),
            dateAndTime.get(Calendar.DAY_OF_MONTH));

    setDateDialog.setTitle("Set Date");
    setDateDialog.show();

}

public void setStartTime() {

    TimePickerDialog setStartTimeDialog = new TimePickerDialog(Shifts.this,
            startTimePicker, dateAndTime.get(Calendar.HOUR),
            dateAndTime.get(Calendar.MINUTE), true);

    setStartTimeDialog.setTitle("Started At:");
    setStartTimeDialog.show();

}

public void setEndTime() {

    TimePickerDialog setEndTimeDialog = new TimePickerDialog(Shifts.this,
            setEndTime, dateAndTime.get(Calendar.HOUR),
            dateAndTime.get(Calendar.MINUTE), true);
    setEndTimeDialog.setTitle("Ended:");
    setEndTimeDialog.show();
}

TimePickerDialog.OnTimeSetListener startTimePicker = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        startIntHours = hourOfDay;
        startIntMinutes = minute;
        editStartTime = String.format("%02d", hourOfDay) + ":"
                + String.format("%02d", minute);
        setEndTime();

    }
};

TimePickerDialog.OnTimeSetListener setEndTime = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

        finishIntHours = hourOfDay;
        finsihIntMinutes = minute;

        if (finishIntHours < startIntHours) {
            finishIntHours = finishIntHours + Utility.HOURS_TIME_UNIT;
        }

        if (finsihIntMinutes < startIntMinutes) {
            finsihIntMinutes = finsihIntMinutes + Utility.MINUTES_TIME_UNIT;
        }

        totalHours = finishIntHours - startIntHours;
        totalMinutes = finsihIntMinutes - startIntMinutes;
        Log.i("TotalHours in time picker", "" + totalHours);
        Log.i("Totalminute in time picker", "" + totalMinutes);

        editEndTime = String.format("%02d", hourOfDay) + ":"
                + String.format("%02d", minute);

        replace(Shifts.view, Shifts.position);

    }
};

Answer

According to your code, neither of those methods are ever called, because you never use the TimePickerDialogs.

That being said, there is a known issue related to the behavior of DatePickerDialog/TimePickerDialog that may be relevant: http://code.google.com/p/android/issues/detail?id=34833

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