Showing posts with label snippet. Show all posts
Showing posts with label snippet. Show all posts

Friday, September 16, 2011

Android Programming - Fast Way Preserving Sqlite Db

Actually this is some snippet I found from browsing, the guy who must be credited to this code is Omar Flores, sorry I can't found your link pal :).
It helping me to copy already made SQLite database to my application, it make my job simpler, do not need to fill the database by coding in my Db Helper. And it really fast too, no lag in the runtime.
public class assets extends Activity
 {
        @Override
        protected void onCreate(Bundle icicle)    {
            super.onCreate(icicle);
            // use your own layout :p
            setContentView(R.layout.main);
            try {
                // Open the file
                InputStream in = getAssets().open("yourDB");
                // Open the output file, remember the package name :)
                String outFilename = "/data/data/com.assets/databases/yourDB";
                OutputStream out = new FileOutputStream(outFilename);
                // Transfer bytes from the input file to the output file
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                // Close the streams
                out.close();
                in.close();
            } catch (IOException e) {
                }
            Toast(this, "File Copied", TOAST.SHORT).show;
        }

 }


Simple code isn't it? But really helpful for me. In my application then I make some additional code to make it just run once, when application first started after install. ^_^

Categories: , , , , ,

Sunday, September 11, 2011

Android Programming - Spinner from Database

Spinner is Android UI that looks like dropdown menu in another UI. Like in the other programming language we can directly add Spinner content directly, by hard coding,not dynamic.
To add spinner content dynamically you need CursorAdapter. I'll make my spinner load content dynamically with SimpleCursorAdapter.

Cursor cClass = dbHelper.fetchAllClass();
        startManagingCursor(cClass);
        SimpleCursorAdapter scaClass = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cClass, new String[] {GSDbAdapter.KEY_CLASS}, new int[]{android.R.id.text1});
        scaClass.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        classSpinner = (Spinner) findViewById(R.id.id_class);
        classSpinner.setAdapter(scaClass);

Cursor cClass come from database query fetchAllClass, then I create SimpleCursorAdapter named scaClass. This adapter will use template from default android layout simple_spinner_item, containing value from KEY_CLASS column in database that will be placed to TextView with id text1.
Then I set the drop drown view resource using layout simple_spinner_dropdown and finally find the spinner Id and add the SimpleCursorAdapter to it.

Note : to make this code work you need database helper class and tempate for your activity. :)

Categories: , , , , , , ,

Android Programming - Override Back Button

Overriding back button is much more simple than options menu before.

@Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("You've pressed the back button\nThanks.")
               .setTitle("Back Pressed")
               .setCancelable(true);
        builder.create().show();
    }

That's it. You're done. It create an AlertDialog when back button pressed.

Categories: , , , , ,

Android Programming - Override Options Menu

To override options menu simply add this code to your activity.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.listmenu, menu);
        return true;
    }



@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Overriding Options Menu\n(c) 2011")
                   .setTitle("Menu Pressed")
                   .setCancelable(true);
            builder.create().show();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

The first section is the overriding of menu button, whenever menu button pressed, it will call you menu from listmenu.xml that placed in the folder res/menu.
Then the second section is the function that run when user click your options menu. For this code, I will create an AlertDialog that shows some text with close button in it.
Don't forget to create your listmenu.xml. Here is the example.
<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/about" android:title="About"></item>
</menu>

Categories: , , , , ,

Copyright © Johannes Dwi Cahyo | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net      Up ↑