Month: December 2012

Example factorial in Python using coroutine generator

Example factorial in Python using coroutine generator

I trying to remember what is factorial then I open wikipedia and found that factorial is all the positive integer that less than or equal with n. Here is a quick implementation in Python using generator: 123456789101112def factorial(n):     while (n > 0):         yield n         n …

+ Read More

Android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

Android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

If we got this errors : 1Android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 What we want? We want to create a new records and read it. Problem? Data is created but we can’t read the new saved records. Solution? using “moveToFirst()”. Here is the example: 123456789101112131415161718192021222324252627282930313233public Cursor createNewRecord() {         …

+ Read More

Check if Cursor data is exists or not with SQLite database in Android

Check if Cursor data is exists or not with SQLite database in Android

When creating a data, sometimes we need to check whether there is a previous data that can be updated or not. To check if data is exists or not, we can use “cursor.moveToFirst()” instead of “cursor.getCount() > 0”. Simple example : 123456789// Check if days is exists mCursor = database.query(dbHelper.TABLE_TRACKER,         allColumns, …

+ Read More

Get the today date in string format in Java

Get the today date in string format in Java

Date() in java.utils provide Date instance which contains the date of today. We can showing the day, month, year of today from Date() instance using SimpleDateFormat().format(). Example: 1String today = new SimpleDateFormat("yyyyMMdd_HH:mm:ss").format(new Date()); Simple!

Convert String to Integer and Integer to String in Java vice versa

Convert String to Integer and Integer to String in Java vice versa

To convert Integer into String in Java, we can use toString() method in Integer class instance : 12Integer hello = 1; String convert = Integer.toString(hello); Then, to convert String into Integer, we can use parseInt() method in Integer class instance: 12String hello = "1"; Integer convert = Integer.parseInt(hello); Easy! 🙂

Example Dialog box in Android

Example Dialog box in Android

Dialog is the good way to providing floating menu in Android which users can do several actions on there. It’s different with Toast which is have purpose for showing information only. At this example, we can try which showing selection menu in Dialog box. For instance, We will make Users can click Button and Dialog …

+ Read More

Immediate update item cells in GridView android

Immediate update item cells in GridView android

We can update immediately background color in items cells (on visibile view) by calling invalidateViews() after do the update. Here is the implementation : 123456789101112131415161718192021222324// Set image adapter instance from private variable imageAdapter = new ItemAdapter(this); // Bind Gridview with calendar XML and set the Adapter final GridView gridView = (GridView) findViewById(R.id.calendar); gridView.setAdapter(imageAdapter); Button workout …

+ Read More

Solve problem wrong update views background color cells in GridView android

Solve problem wrong update views background color cells in GridView android

Usually we need to update item in GridView programmatically without using “ClickListener”. Instead, we can set a “selected position” variable in our Adapter and do position matching in getView(). Usually, our code will end-up like this: 1234567891011121314151617181920212223242526272829303132public void setChoosenDate(int date) {     this.selectedDate = date; } @Override public View getView(int position, View convertView, ViewGroup …

+ Read More

How to set FULLSCREEN application window in Android

How to set FULLSCREEN application window in Android

We can set our application into FULL SCREEN mode which will show in entire Android monitor. To do it, we just need to declare FLAG_FULLSCREEN in our Activity. For instance: 123456789@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     // Remove application title     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,               …

+ Read More

Be careful when validate input using length() in Javascript

Be careful when validate input using length() in Javascript

This is trivial things but sometimes just happen. For instance we want to validate the input from users. Fyi, I use JQuery example here. 1var amount = $(".amount p").text(); When we want to validate amount when we pass this variable into function, usually we use: 123if(amount.length > 0) {      // logic here } …

+ Read More

Unit Test in Django doesn’t running the test method inside the TestCase

Unit Test in Django doesn’t running the test method inside the TestCase

If you got the problem like what I mention on the title, that’s mean you need to take a rest. Why? The big possibilty is sometimes we forgot to add “test” as prefix on our testCase method. For instance: 12345678class InsuranceTestCase(TestCase):     """Insurance unit-testing"""     def setUp(self):         pass   …

+ Read More

Solve django admin search error related fields has invalid lookup: icontains

Solve django admin search error related fields has invalid lookup: icontains

When we trying to do search in Django admin, suddenly we caught by this error: 12TypeError at /admin/myapp/myappfolder/ Related Field has invalid lookup: icontains Then we should check that search_fields in DjangoAdmin should be on field rather on ForeignKey objects. Eg : 12345class InsuranceAdmin(admin.ModelAdmin):     """Set Insurance Configuration in Admin page"""     list_display …

+ Read More

Django suddenly get very heavy and slow

Django suddenly get very heavy and slow

Today i found something weird with my django applications. Suddenly, it became heavy and slow. It took 100 to 150 % of Python process. Sometimes I saw this error : 12345678910111213141516Traceback (most recent call last):   File "/home/tripvillas/.virtualenvs/trip/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 284, in run     self.finish_response()   File "/home/tripvillas/.virtualenvs/trip/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 324, in finish_response     self.write(data) …

+ Read More

Build Calendar Applications with TODO List and Activity in Android Part 2

Build Calendar Applications with TODO List and Activity in Android Part 2

Yes, i’ve made progress in building my Android applications. There is a new things I’ve learned today, especially on design. I found out that RelativeLayout is powerfull and flexible to build blocks. Here is the Part 2 results : You can grab this on my github : https://github.com/yodiaditya/android Previous: http://www.yodi.biz/2012/12/08/build-calendar-applications-with-todo-list-and-activity-in-android-part-1/ Next update: http://www.yodi.biz/2012/12/16/immediate-update-item-cells-in-gridview-android/

Solve [error] 28901#0: *401 upstream sent too big header while reading response header from upstream

Solve [error] 28901#0: *401 upstream sent too big header while reading response header from upstream

Today I got this 502 error on my websites : 1Solve [error] 28901#0: *401 upstream sent too big header while reading response header from upstream, client: 116.86.214.27, server: yodi.biz, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "yodi.biz" After looking at NGINX documentation, i found the problem in my fastcgi buffers. So, the solution is just …

+ Read More

Android : How to disable or remove application name title bar

Android : How to disable or remove application name title bar

When building Android application, it will have Application Title on top bar as default. We can remove this app title bar with 2 ways. Here is how to remove title bar: 1. Using AndroidManifest.XML In the Activity, we can set the Theme using NoTitleBar, like this: 12345<activity      android:name="com.yodi.calendar.MainActivity"      android:theme="@android:style/Theme.Black.NoTitleBar"     …

+ Read More

Example click on detail item using intent and use another Activity in Android

Example click on detail item using intent and use another Activity in Android

The usual things when we deploy application in Android is providing detail information after user click on some parts of our application. Given example is calendar, which have MainActivity like this: MainActivity.java 1<br /> package com.yodi.calendar; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.GridView; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity …

+ Read More

Convert date into datetime and reverse datetime into date objects in Python

Convert date into datetime and reverse datetime into date objects in Python

This is a quick tips to convert “datetime” into “date” objects in Python: 12345from datetime import datetime today = datetime.now() print today today_date = today.date() print today_date The result will be : 12datetime.datetime(2012, 12, 6, 14, 48, 5, 133765) datetime.date(2012, 12, 6) Then, how about convert date into datetime objects? We need time() function: 123456from …

+ Read More

How to set Django admin models into Read Only mode

How to set Django admin models into Read Only mode

This is common question we need to showing models of app inside django admin but in read-only mode. That’s meaning admin only can see but can’t modify. For instance, I have models called Insurance : 1234class InsuranceBranch(models.ModelAdmin):     country = models.ForeignKey(Country)     created = models.DateTimeField(auto_now_add=True, default=datetime.now())     modified = models.DateTimeField(auto_now=True, default=datetime.now()) Then, …

+ Read More