vendredi 11 mars 2016

in my android application sign up and sign in using SQLite open helper i have an error in my code please curet it

In my android application I am using sqllite open helper class I am creating sign up activity and login activity. In sign up activity name,password,phone number there fields are saved in data base.and next in sign in activity how to stored data to using that SQL data name and password how to sign in please tell me the login activity code how to getting that SQL data in log in activity?............... please help me . And also how to check the data stored in data base or not in signup activity?....thank you.

MySQLiteHelper.java file``

public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_USERS = "users";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRST_NAME = "firstname";
public static final String COLUMN_LAST_NAME = "lastname";
public static final String COLUMN_AGE = "age";
private static final String DATABASE_NAME = "users.db";
private static final int DATABASE_VERSION = 1;
SQLiteDatabase db;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_USERS + "( " + COLUMN_ID
        + " integer primary key autoincrement, " + COLUMN_FIRST_NAME
        + " text not null," + COLUMN_LAST_NAME + " text not null," +
        COLUMN_AGE + " text not null);";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
    this.db = db;
}
public void insertUsers(Users u){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    String query = "select * from users";
    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();
    values.put(COLUMN_ID, count);
    values.put(COLUMN_FIRST_NAME, u.getmFirstName());
    values.put(COLUMN_LAST_NAME, u.getmLastName());
    values.put(COLUMN_AGE, u.getmAge());
    db.insert(TABLE_USERS, null, values);
    db.close();
}
public List<Users> getSinlgeEntry(String firstname, String lastname)
{
    List<Users> userList = new ArrayList<Users>();
    // Select All Query
    String selectQuery = "SELECT * FROM users";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
    do {
        Users user = new Users();
        user.setmFirstName(cursor.getString(0));
        user.setmLastName(cursor.getString(1));
    // Adding contact to list
    userList.add(user);
    } while (cursor.moveToNext());
    }
    // close inserting data from database
    db.close();
    // return contact list
    return userList;
    }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query ="DROP TABLE IF EXISTS "+TABLE_USERS;
    db.execSQL(query);
    this.onCreate(db);  
}       
}


SignUpActivity.java  file

public class SignUpActivity extends Activity  {
MySQLiteHelper helper = new MySQLiteHelper(this);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);
}   

public void onSignUpClick(View v){
    if (v.getId() == R.id.btnSignupRegister){
        EditText fname = (EditText) findViewById(R.id.edt_Name);
        EditText lname = (EditText) findViewById(R.id.edt_emailAddress);
        EditText age = (EditText) findViewById(R.id.edt_userName);
        String firstnamestr= fname.getText().toString();
        String lastnamestr = lname.getText().toString();
        String agestr = age.getText().toString();
        if (!agestr.equals(lastnamestr))
        {
            //save data into database
            Users user = new Users(); 
            //user.setmId(cursor.getLong(0));
            user.setmFirstName(firstnamestr);
            user.setmLastName(lastnamestr);
            user.setmAge(agestr);
            helper.insertUsers(user);
            Toast temp = Toast.makeText(getApplicationContext(),     "Successfully Registered", Toast.LENGTH_SHORT);
            temp.show();
            Intent i=new Intent(SignUpActivity.this, MainActivity.class);
            startActivity(i);
        }
        else
        {   
            //pop up
            Toast pass = Toast.makeText(SignUpActivity.this, "Fill the data !", Toast.LENGTH_SHORT);
            pass.show();    
}
}
}
}

Aucun commentaire:

Enregistrer un commentaire