Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

android - Unable to register in the Registration(Table not getting inserted)

I have tried to create registration and a login page.But when I am trying to register,the field values are not getting inserted.This is my code

MainActivity.java

package com.example.login1;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity implements OnClickListener 
{

    Button mLogin;
    Button mRegister;

    EditText muname;
    EditText mpassword;

    DBHelper DB = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mRegister = (Button) findViewById(R.id.register);
        mRegister.setOnClickListener(this);

        mLogin = (Button) findViewById(R.id.login);
        mLogin.setOnClickListener(this);

    }

    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.register:
            Intent i = new Intent(getBaseContext(), Registration.class);
            startActivity(i);
            break;

        case R.id.login:

            muname = (EditText) findViewById(R.id.Ledituname);
            mpassword = (EditText) findViewById(R.id.Leditpw);

            String username = muname.getText().toString();
            String password = mpassword.getText().toString();

            if (username.equals("") || username == null) {
                Toast.makeText(getApplicationContext(),
                        "Please enter User Name", Toast.LENGTH_SHORT).show();
            } else if (password.equals("") || password == null) {
                Toast.makeText(getApplicationContext(),
                        "Please enter your Password", Toast.LENGTH_SHORT)
                        .show();
            } else {
                boolean validLogin = validateLogin(username, password,
                        getBaseContext());
                if (validLogin) {
                    // System.out.println("In Valid");
                    //Intent in = new Intent(getBaseContext(), TabBar.class);
                    // in.putExtra("UserName", muname.getText().toString());
                    //startActivity(in);
                    Intent i1 = new Intent(getBaseContext(), Welcome.class);
                    Toast.makeText(getApplicationContext(),
                            "Success Valid Login", Toast.LENGTH_SHORT)
                            .show();
                    startActivity(i1);
                    // finish();
                }
            }
            break;

        }

    }

    private boolean validateLogin(String username, String password,
            Context baseContext) {
        DB = new DBHelper(getBaseContext());
        SQLiteDatabase db = DB.getReadableDatabase();

        String[] columns = { "_id" };

        String selection = "username=? AND password=?";
        String[] selectionArgs = { username, password };

        Cursor cursor = null;
        try {

            cursor = db.query(DBHelper.DATABASE_TABLE_NAME, columns, selection,
                    selectionArgs, null, null, null);
            startManagingCursor(cursor);
        } catch (Exception e)

        {
            e.printStackTrace();
        }
        int numberOfRows = cursor.getCount();

        if (numberOfRows <= 0) {

            Toast.makeText(getApplicationContext(),
                    "User Name and Password miss match..
Please Try Again",
                    Toast.LENGTH_LONG).show();
            Intent intent = new Intent(getBaseContext(), MainActivity.class);
            startActivity(intent);
            return false;
        }

        return true;

    }

    public void onDestroy() {
        super.onDestroy();
        DB.close();
    }
}

Registration.java

package com.example.login1;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Registration extends Activity implements OnClickListener,
OnItemSelectedListener  
{
    private Button mSubmit;
    private Button mCancel;

    private EditText mFname;
    private EditText mLname;
    private Spinner mGender;
    private EditText mEmail;
    private EditText mPhno;
    private EditText mAdd;
    private EditText mBloodGroup;
    private EditText mDob;
    private EditText mFatherName;
    private EditText mMotherName;
    private EditText mUsername;
    private EditText mpass;
    private EditText mcpass;

    private String Gen;

      protected DBHelper DB = new DBHelper(Registration.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

         // Assignment of UI fields to the variables
        mSubmit = (Button) findViewById(R.id.submit);
        mSubmit.setOnClickListener(this);

        mCancel = (Button) findViewById(R.id.cancel);
        mCancel.setOnClickListener(this);

        mFname = (EditText) findViewById(R.id.efname);
        mLname = (EditText) findViewById(R.id.elname);
        mGender = (Spinner) findViewById(R.id.spinner1);
        mEmail = (EditText) findViewById(R.id.eemail);
        mPhno = (EditText) findViewById(R.id.ephnno);
        mAdd = (EditText) findViewById(R.id.eadd);
        mBloodGroup = (EditText) findViewById(R.id.ebloodGroup);
        mDob = (EditText) findViewById(R.id.edob);
        mFatherName = (EditText) findViewById(R.id.eFatherName);
        mMotherName = (EditText) findViewById(R.id.eMotherName);
        mUsername = (EditText) findViewById(R.id.eusername);
        mpass = (EditText) findViewById(R.id.epass);
        mcpass = (EditText) findViewById(R.id.ecpass);

        // Spinner method to read the on selected value
        ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
                android.R.layout.simple_spinner_item, new State[] {
                        new State("Male"), new State("Female") });
        mGender.setAdapter(spinnerArrayAdapter);
        mGender.setOnItemSelectedListener(this);
    } 
        @Override
        public void onClick(View v) {

            switch (v.getId()) {

            case R.id.cancel:
                Intent i = new Intent(getBaseContext(),MainActivity.class);
                startActivity(i);
                // finish();
                break;

            case R.id.submit:

                 String fname = mFname.getText().toString();
                 String lname = mLname.getText().toString();
                 String email = mEmail.getText().toString();
                 String phoneno = mPhno.getText().toString();

                 String address = mAdd.getText().toString();
                 String bloodgroup = mBloodGroup.getText().toString();
                 String dob = mDob.getText().toString();
                 String father = mFatherName.getText().toString();

                 String mother = mMotherName.getText().toString();
                 String username = mUsername.getText().toString();
                 String password = mpass.getText().toString();
                 String cpassword= mcpass.getText().toString();

                boolean invalid = false;

                if (fname.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(), "Enter your Firstname",
                            Toast.LENGTH_SHORT).show();
                } else

                if (lname.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your Lastname", Toast.LENGTH_SHORT)
                            .show();
                } else

                if (email.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your Username", Toast.LENGTH_SHORT)
                            .show();
                } else

                if (phoneno.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your Password", Toast.LENGTH_SHORT)
                            .show();

                } else if (address.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your Email ID", Toast.LENGTH_SHORT)
                            .show();
                } 
                else if (bloodgroup.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your phone", Toast.LENGTH_SHORT)
                            .show();
                }
                else if (dob.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your phone", Toast.LENGTH_SHORT)
                            .show();
                }
                else if (father.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your phone", Toast.LENGTH_SHORT)
                            .show();
                }
                else if (mother.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your phone", Toast.LENGTH_SHORT)
                            .show();
                }
                else if (username.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your phone", Toast.LENGTH_SHORT)
                            .show();
                }
                else if (password.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your phone", Toast.LENGTH_SHORT)
                            .show();
                }
                else if (cpassword.equals("")) {
                    invalid = true;
                    Toast.makeText(getApplicationContext(),
                            "Please enter your phone", Toast.LENGTH_SHORT)
                            .show();
                }

                else if (invalid == false) {
                    addEntry(fname, lname, Gen, email,phoneno,address,bloodgroup,dob,father,mother,username,password,cpassword);
                    Intent i_register = new Intent(Registration.this,Ma

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Error says clearly: table igate1 has no column named password.

Your Create query is:

private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
                + DATABASE_TABLE_NAME
                + "("
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "firstname TEXT NOT NULL, lastname TEXT NOT NULL, gender TEXT NOT NULL,email TEXT NOT NULL, phoneno TEXT NOT NULL, address TEXT NOT NULL,bloodgroup TEXT NOT NULL, dob TEXT NOT NULL,father TEXT NOT NULL, mother TEXT NOT NULL, username TEXT NOT NULL, pass TEXT NOT NULL, cpass TEXT NOT NULL );";

And there are fields pass and cpass, not password.

Change that, and you're done.

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...