Pages

Wednesday 2 May 2012

Cooking Vs Coding


                      For me both cooking and coding are same.Yes we  need to think it in efficient way. If anything is missed out the output will be different :P. yeah I love both cooking and coding. The ingredients and parameters are same. The way that used in the main procedure should be in the correct order, should be in efficient way. If we used int parameter as string will create a bug , as same as if add red chilli instead of green chilli will make a different taste. Then next thing is, the way we are coding is very much important . so many things we need to do concentrate while coding such as time complexity, efficiency etc. like that in cooking the way we are cooking will produce the delicious food. The efficiency in cooking is that ingredients must be healthy one. The cooking should be done in as much as in minimum time:) so for me both coding and cooking are same. I am enjoying the both.

Friday 30 March 2012

Samsung Force close Exception


Here is the solution for the mobile(Samsung galaxy fit, Samsung galaxy y ) which shows the exception always.
Steps are followed
1       .     Switch off the mobile
2       .     Hold power button and Home button(center button)
3       .     Screen reboot will come
4       .     Click the 3rd option(something like wipe out )
5      .     It will navigate to the next screen
6       .     Enter the reboot option
7       .     Now you can switch on the mobile and can access as normal as you will.

Monday 19 March 2012

Call activity from class


In android I have faced another problem which is nothing but how to call activity from class.Usually we came across with calling activity from one activity to another activity and calling class from activity. But when I came in that situation to call an activity from class I struggled a lot to solve it.

Hope the following is the code for call an activity from class

Intent spreadSheetIntent = new Intent(con,
                              SpreadSheetActivity.class);
                  con.startActivity(spreadSheetIntent);

In the above example, You have to create Intent instance as normal like calling an activity from another activity.But the thing is the first parameter should be the context  of your application. If you are giving the first parameter as currentclassname.this which means that context of that corresponding class.

Where second parameter is an activity that you want to display/ want to navigate.
Here con is nothing but context of the application. I made con as global.Once my application is started , I assigned my application context to con. So wherever i want to use my application context I can con.

And finally start your activity as normal as using intent J

Thursday 15 March 2012

Custom Content Provider


This will be useful for creating the own content provider. Follow the steps which has given below to create your own custom adapter. I thought that content provider and sqlite database are different. But what i thought is totally worng. The database can access only from that particular application. If you are creating the content provider which means creating URI for each row for the database. So from other application you can access tha database using the content provider URI. So both are inter related . You will understand if you follow the code.
Mimeprovider.java
package com.Androitanz.Provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * The provider creation,fetching and displaying
 *
 * @author Anitha
 *
 */
public class MimeProvider extends ContentProvider {
      /**
       * The instance sqlitedatabase
       */
      private SQLiteDatabase sqlDB;
      /**
       * The instance for DatabaseHelper
       */
      private DatabaseHelper dbHelper;
      /**
       * Database name for MIME app
       */
      private static final String DATABASE_NAME = "Friends.db";
      /**
       * Database version that we are using
       */

      private static final int DATABASE_VERSION = 1;
      /**
       * The table name using to store the information
       */

      private static final String TABLE_NAME = "Friends";

      private static final String TAG = "AndrointanZProvider";

      /**
       *
       * @author Lukshanu
       *
       */
      public static class ProviderDetails implements BaseColumns {
            public static final String AUTHORITY = "com.Androitanz.Provider.MimeProvider";

            // BaseColumn contains _id.

            public final static Uri CONTENT_URI = Uri
                        .parse("content://com.Androitanz.Provider.MimeProvider");

            // Table column
            public static final String FRIENDS_NAME = "FRIENDS_NAME";
      }

      private static class DatabaseHelper extends SQLiteOpenHelper {

            DatabaseHelper(Context context) {
                  super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                  // create table to store user names
                  db.execSQL("Create table "
                              + TABLE_NAME
                              + "( _id INTEGER PRIMARY KEY AUTOINCREMENT, FRIENDS_NAME TEXT);");
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                  db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                  onCreate(db);
            }
      }

      @Override
      public int delete(Uri uri, String selection, String[] selectionArgs) {
            // TODO Auto-generated method stub

            return 0;
      }

      @Override
      public String getType(Uri uri) {
            // TODO Auto-generated method stub
            return null;
      }

      @Override
      public Uri insert(Uri uri, ContentValues contentValues) {
            // get database to insert records
            sqlDB = dbHelper.getWritableDatabase();
            // insert record in user table and get the row number of recently
            long rowId = sqlDB.insert(TABLE_NAME, "", contentValues);
            if (rowId > 0) {
                  Uri rowUri = ContentUris.appendId(
                              ProviderDetails.CONTENT_URI.buildUpon(), rowId).build();
                  getContext().getContentResolver().notifyChange(rowUri, null);
                  return rowUri;
            }
            throw new SQLException("Failed to insert row into " + uri);
      }

      @Override
      public boolean onCreate() {
            dbHelper = new DatabaseHelper(getContext());
            return (dbHelper == null) ? false : true;
      }

      @Override
      public Cursor query(Uri uri, String[] projection, String selection,
                  String[] selectionArgs, String sortOrder) {
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            qb.setTables(TABLE_NAME);
            Cursor c = qb.query(db, projection, selection, null, null, null,
                        sortOrder);
            c.setNotificationUri(getContext().getContentResolver(), uri);
            return c;
      }

      @Override
      public int update(Uri uri, ContentValues values, String selection,
                  String[] selectionArgs) {
            // TODO Auto-generated method stub
            return 0;
      }

}
The Activity to get the inputs and to store it in the content provider.
ContentDisplay.java
package com.Androitanz.Activity;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.Androitanz.Provider.MimeProvider.ProviderDetails;

/**
 * The Screen to fetch the name to store it in the provider and database
 *
 * @author Anitha
 *
 */
public class ContentDisplay extends Activity {
      private EditText mContactNameEditText;
      private TextView mContactsText;
      private Button mContactSaveButton;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            // Obtain handles to UI objects
            mContactNameEditText = (EditText) findViewById(R.id.contactNameEditText);
            mContactSaveButton = (Button) findViewById(R.id.contactSaveButton);
            mContactsText = (TextView) findViewById(R.id.contactEntryText);
            mContactSaveButton.setOnClickListener(new View.OnClickListener() {

                  @Override
                  public void onClick(View v) {
                        String name = mContactNameEditText.getText().toString();
                        insertRecord(name);
                        mContactsText.append("\n" + name);
                  }
            });

            displayRecords();
      }

      private void insertRecord(String userName) {
            ContentValues values = new ContentValues();
            values.put(ProviderDetails.FRIENDS_NAME, userName);
            getContentResolver().insert(ProviderDetails.CONTENT_URI, values);
      }

      private void displayRecords() {
            // An array specifying which columns to return.
            String columns[] = new String[] { ProviderDetails._ID,
                        ProviderDetails.FRIENDS_NAME };
            Uri myUri = ProviderDetails.CONTENT_URI;
            Cursor cur = managedQuery(myUri, columns, // Which columns to return
                        null, // WHERE clause; which rows to return(all rows)
                        null, // WHERE clause selection arguments (none)
                        null // Order-by clause (ascending by name)
            );
            if (cur.moveToFirst()) {
                  String id = null;
                  String userName = null;
                  do {
                        // Get the field values
                        id = cur.getString(cur.getColumnIndex(ProviderDetails._ID));
                        userName = cur.getString(cur
                                    .getColumnIndex(ProviderDetails.FRIENDS_NAME));
                        Toast.makeText(this, id + " " + userName, Toast.LENGTH_LONG)
                                    .show();
                  } while (cur.moveToNext());
            }
      }
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    <TableLayout android:layout_width="fill_parent"
                 android:layout_height="fill_parent">

        <TableRow>
            <TextView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="contactNameLabel"/>
        </TableRow>
        <TableRow>
            <EditText android:id="@+id/contactNameEditText"
                      android:layout_height="wrap_content"
                      android:layout_width="wrap_content"
                      android:layout_weight="1"/>
        </TableRow>
        <TableRow>
            <Button android:layout_height="wrap_content"
                    android:text="save"
                    android:id="@+id/contactSaveButton"
                    android:layout_width="fill_parent"
                    android:layout_weight="1"/>
        </TableRow>
        <TableRow>
              <TextView android:text="@+id/contactEntryText"
                    android:id="@+id/contactEntryText"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>
             </TableRow>

    </TableLayout>
</ScrollView>
The things need to be mentioned in the manifest file are as follows. The provider tag should come within the application tag
<provider
    android:authorities="com.Androitanz.Provider.MimeProvider"
    android:multiprocess="true"
    android:name="com.Androitanz.Provider.MimeProvider"></provider>