Here is the java activity that provide to add data for record in SQLite. My question is how i insert some code for when clicking button and it will show second activity. i don't know where i should insert. And don't know what the code should be. please help me.
import java.sql.PreparedStatement;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class AddStudent extends Activity {
DatabaseStudent mHelper;
SQLiteDatabase mDb;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
mHelper = new DatabaseStudent(this);
mDb = mHelper.getWritableDatabase();
final EditText editName = (EditText)findViewById(R.id.editName);
final EditText editLastName = (EditText)findViewById(R.id.editLastName);
ImageView buttonAdd = (ImageView)findViewById(R.id.imageAdd);
buttonAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String name = editName.getText().toString();
String lastname = editLastName.getText().toString();
String condition = getIntent().getStringExtra("Condition");
double school = getIntent().getDoubleExtra("Intent", 0);
//Date&Time
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
if(name.length() != 0 && lastname.length() != 0
) {//&& school.length() != 0
Cursor mCursor = mDb.rawQuery("SELECT * FROM "
+ DatabaseStudent.TABLE_NAME + " WHERE "
+ DatabaseStudent.COL_NAME + "='" + name + "'"
+ " AND " + DatabaseStudent.COL_LASTNAME + "='"
+ lastname + "'" + " AND "
+ DatabaseStudent.COL_SCHOOL + "='" + school //add COL_SCHOOL = currentTime
+ "'"+ " AND " + DatabaseStudent.COL_TIME + "='" + currentTime
+ "'"+ " AND " + DatabaseStudent.COL_CON + "='" + condition
+ "'", null);
if(mCursor.getCount() == 0) {
mDb.execSQL("INSERT INTO " + DatabaseStudent.TABLE_NAME
+ " (" + DatabaseStudent.COL_NAME
+ ", " + DatabaseStudent.COL_LASTNAME
+ ", " + DatabaseStudent.COL_SCHOOL
+ ", " + DatabaseStudent.COL_TIME
+ ", " + DatabaseStudent.COL_CON
+ ") VALUES ('" + name + "', '" + lastname
+ "', '" + school + "', '" + currentTime + "', '" + condition + "');");
editName.setText("");
editLastName.setText("");
Toast.makeText(getApplicationContext()
, "Already added"
, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext()
, "This data is exist"
, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext()
, "Please fill in the blank"
, Toast.LENGTH_SHORT).show();
}
}
});
}
public void onStop() {
super.onStop();
mHelper.close();
mDb.close();
}
}