0

I am new to android database. I'm creating an application following a youtube tutorial about database http://www.youtube.com/watch?v=TApjQ-LFNpI.

I followed everything and, when I run the application there were no errors but no response on the create button when clicking.

here is My code:

MainActivity.java

package com.example.studentdatabase;


import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {
    String fname,lname,email;
    SQLiteDatabase db;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db= openOrCreateDatabase("MyDB1",MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname TEXT,lname TEXT,email TEXT)");
    }

        public void Adddata(View view){

        EditText editText1=(EditText) findViewById(R.id.firstname);
        EditText editText2=(EditText) findViewById(R.id.lastname);
        EditText editText3=(EditText) findViewById(R.id.email);

        fname=editText1.getText().toString();
        lname=editText2.getText().toString();
        email=editText3.getText().toString();
        db.execSQL("INSERT INTO Student VALUES('"+fname+"','"+lname+"','"+email+"')");


    }   

    public void showdata(View view){

        Cursor c=db.rawQuery("SELECT * from Student", null);
        int count=c.getCount();
        c.moveToFirst();
        TableLayout tableLayout=new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
        TableRow tableRow;
        TextView textView,textView1,textView2,textView3,textView4,textView5;
        tableRow=new TableRow(getApplicationContext());

        textView=new TextView(getApplicationContext());
        textView.setText("Firstname");
        textView.setTextColor(Color.RED);
        textView.setTypeface(null,Typeface.BOLD);
        textView.setPadding(20, 20, 20, 20);
        tableRow.addView(textView);

        textView4=new TextView(getApplicationContext());
        textView4.setText("LastName");
        textView4.setTextColor(Color.RED);
        textView4.setTypeface(null,Typeface.BOLD);
        textView4.setPadding(20, 20, 20, 20);

        textView5=new TextView(getApplicationContext());
        textView5.setText("LastName");
        textView5.setTextColor(Color.RED);
        textView5.setTypeface(null,Typeface.BOLD);
        textView5.setPadding(20, 20, 20, 20);
        tableRow.addView(tableRow);

        for(Integer j=0; j < count; j++){
            tableRow=new TableRow(getApplicationContext());
            textView1=new TextView(getApplicationContext());
            textView1.setText(c.getString(c.getColumnIndex("fname")));
            textView2=new TextView(getApplicationContext());
            textView2.setText(c.getString(c.getColumnIndex("lname")));
            textView3=new TextView(getApplicationContext());
            textView3.setText(c.getString(c.getColumnIndex("email")));

            textView1.setPadding(20, 20,20, 20);
            textView2.setPadding(20, 20,20, 20);
            textView3.setPadding(20, 20,20, 20);

            tableRow.addView(textView1);
            tableRow.addView(textView2);
            tableRow.addView(textView3);

            tableLayout.addView(tableRow);
            c.moveToNext();




        }
        setContentView(tableLayout);
        db.close();
        }
        public void close(View view){

            System.exit(0);
        }




}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="25dp"
        android:text="last Name" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25dp"
        android:text="Email" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="27dp"
        android:text="First Name" />

    <EditText
        android:id="@+id/firstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/lastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/firstname"
        android:ems="10" />

    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/email"
        android:layout_marginTop="28dp"
        android:onClick="Adddata"
        android:text="create" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/email"
        android:layout_marginRight="42dp"
        android:onClick="showdata"
        android:text="Show Table" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="33dp"
        android:onClick="close"
        android:text="Close" />

</RelativeLayout>
user46648
  • 11
  • 1
  • 6
  • 1
    `System.exit(0);` why do you need this? http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon – Raghunandan Jan 16 '14 at 16:53
  • 1
    crash => stacktrace. Add it to your question. – Alexis C. Jan 16 '14 at 16:54
  • Can you put `try { ... } Catch () ` around your db access ? – avrono Jan 16 '14 at 16:55
  • Your tutorial is not good. Instead of `openOrCreateDatabase` + creating tables in your `Activity` use `SQLiteOpenHelper` like in http://www.vogella.com/tutorials/AndroidSQLite/article.html – zapl Jan 16 '14 at 16:59
  • If your app crashes and there are no **syntactical** errors, there must be some **logical** errors. – Phantômaxx Jan 16 '14 at 17:01
  • than you so much for your Quick Help this is my Youtube tutorial By the way http://www.youtube.com/watch?v=TApjQ-LFNpI – user46648 Jan 16 '14 at 17:03

1 Answers1

-1

I don't see any Button or onClick event in your code?! Was you sure to setup event for your button?

ribbon
  • 11
  • 3
  • Sir Tobot..I appreciate your help..thank you sir.. I just need some help so that I could have a better understanding on database..This codes for me is easy to understand thats why I used the tutorial and found some errors I hope stack could help me in correcting the codes.. – user46648 Jan 16 '14 at 17:13
  • It's OK. There's nothing wrong in defining listeners in xml. And there's nothing wrong to use queries and statements to manipulate a database - They are both my preferred ways of doing things like these. – Phantômaxx Jan 16 '14 at 18:01