Android Open Source - Use-Sqlite-in-android Main Activity






From Project

Back to project page Use-Sqlite-in-android.

License

The source code is released under:

Copyright 2014 farzad farazmand

If you think the Android project Use-Sqlite-in-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.farazmand.testsqllite;
// w  w  w .  j  av a2s  .c  o m
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;


public class MainActivity extends Activity {

    public String[] str;
    public static SQLiteDatabase database;
    public static final String DIR_SD_CARD = Environment.getExternalStorageDirectory().getAbsolutePath();
    public static final String DIR_DATABASE = DIR_SD_CARD + "/databaseTest/";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText edtFirstName = (EditText) findViewById(R.id.edtFirstName);
        final EditText edtLastName = (EditText) findViewById(R.id.edtLastName);
        final EditText edtAge = (EditText) findViewById(R.id.edtAge);

        new File(DIR_DATABASE).mkdirs();
        database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE+"database.sqlite",null);
        database.execSQL("CREATE  TABLE IF NOT EXISTS person (" +
                "person_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , " +
                "person_name TEXT, " +
                "person_family TEXT, " +
                "person_age INTEGER)");
        database.close();
        database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE+"database.sqlite",null);
        Cursor cursorID = database.rawQuery("SELECT * FROM person",null);

        if (cursorID != null){
            Toast.makeText(getApplicationContext(),"not null",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(),"null",Toast.LENGTH_SHORT).show();
        }

                /*ArrayList<String> array = new ArrayList<String>();
                int i=0;
                while (cursorID.moveToNext()) {
                    String str = cursorID.getString(cursorID.getColumnIndex("person_name"));
                    array.add(i, str);
                    i++;

                }*/

        cursorID.moveToLast();
        int num = cursorID.getPosition();
        cursorID.moveToFirst();
        String[] str = new String[num];
        int i =0;
        while (cursorID.moveToNext()){
            str[i] = cursorID.getString(cursorID.getColumnIndex("person_name"));
            i++;
        }
        cursorID.close();
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> a =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,str);
        spinner.setAdapter(a);

        Button btnInsert = (Button) findViewById(R.id.btnInsert);
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String firstName = edtFirstName.getText().toString();
                String lastName = edtLastName.getText().toString();
                int age = Integer.parseInt(edtAge.getText().toString());
                database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE+"/database.sqlite",null);
                database.execSQL("INSERT INTO person (person_name,person_family,person_age) VALUES('"+firstName+"','"+lastName+"',"+age+");");
                database.close();

                edtFirstName.setText("");
                edtLastName.setText("");
                edtAge.setText("");



            }
        });
        Button btnSelect = (Button) findViewById(R.id.btnSelect);
        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {









            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}




Java Source Code List

me.farazmand.testsqllite.ApplicationTest.java
me.farazmand.testsqllite.G.java
me.farazmand.testsqllite.MainActivity.java