The code below is for the MainActivity.java creating save,Display,clear
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
SQLiteDatabase db;
TextView tv;
EditText et1,et2,et3;
Spinner spin;
String[] Gender = { "Female", "Male" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize all view objects
// Spinner spin = (Spinner) findViewById(R.id.spinner1);
// spin.setOnItemSelectedListener(this);
spin=(Spinner)findViewById(R.id.spinner1);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,Gender);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
tv=(TextView)findViewById(R.id.textView1);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
et3=(EditText)findViewById(R.id.editText3);
//create database if not already exist
db= openOrCreateDatabase("University", MODE_PRIVATE, null);
//create new table if not already exist
db.execSQL("create table if not exists student (FirstName varchar, LastName varchar,Telephone varchar,Gender varchar)");
}
//This method will call on when we click on insert button
public void Save(View v)
{
String FirstName=et1.getText().toString();
String LastName=et2.getText().toString();
String Telephone=et3.getText().toString();
String Gender=spin.getContext().toString();
erase();
//insert data into able
db.execSQL("insert into student values('"+FirstName+"','"+LastName+"','"+Telephone+"','"+Gender+"')");
//display Toast
Toast.makeText(this, "Data have been successfully saved.", Toast.LENGTH_LONG).show();
}
// This method is called to clear the 1st three field
public void erase()
{
et1.setText("");// these codes clears data
et2.setText("");
et3.setText("");
}
// This method is clearing only TextView
public void ClearTextView(View v)
{
tv.setText("");
}
//This method will call when we click on display button
public void display(View v)
{
//use cursor to keep all data
//cursor can keep data of any data type
Cursor c=db.rawQuery("select * from student", null);
tv.setText("");
//move cursor to first position
c.moveToFirst();
//fetch all data one by one
do
{
//we can use c.getString(0) here
//or we can get data using column index
String FirstName=c.getString(c.getColumnIndex("FirstName"));
String LastName=c.getString(1);
String Telephone=c.getString(2);
String Gender=c.getString(3);
//display on text view
tv.append("FName=:"+FirstName+"\n LName=:"+LastName+"\nTele=:"+Telephone+"\nGender=:"+Gender+"\n");
//move next position until end of the data
}while(c.moveToNext());
}
}
Download Free Code here!!!!!
goo
ReplyDelete