0

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();
}

}

user3101751
  • 59
  • 2
  • 8
  • 1
    Be better: Avoid all database transaction from Activity – Suzon Feb 21 '14 at 15:45
  • For further details, the following might be useful (although not really directed at beginners) : http://stackoverflow.com/questions/2493331/what-are-the-best-practices-for-sqlite-on-android – 2Dee Feb 21 '14 at 15:58

1 Answers1

2

You'd want to insert any code you want executed when you hit a button in the onClick method, which you have as seen here:

buttonAdd.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

    // do stuff when buttonAdd is clicked

    }
});

Now you can use intents inside the onClick method to begin a second activity, like so:

buttonAdd.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

        Intent intent = new Intent(current_activity.this, second_activity.class);
        startActivity(intent);
    }
});

You can consult the following for more details: http://developer.android.com/training/basics/firstapp/starting-activity.html

jak10h
  • 519
  • 4
  • 11