1

我已经尝试了一切,在这个阶段对我的 android 登录感到非常沮丧,有人可以帮忙吗?我有一个登录屏幕,要求用户输入电子邮件和密码,当他们单击登录时,它需要检查我的sqlite数据库,如果正确,他们将成功登录,如果不正确,他们将被要求重试!我在下面发布了我的代码

登录活动

   package com.example.finalproject;



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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity{

    EditText EmailAdd;
    EditText Password;
    Button Login;
    Button NewUser;
    private SQLiteAdapter db;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        //addListenerOnButton();

        //Button mNewUser = (Button)findViewById(R.id.btnLogMain);
        //mNewUser.setOnClickListener(this);

        EmailAdd = (EditText)findViewById(R.id.password);
        Password = (EditText)findViewById(R.id.password);

        Login = (Button)findViewById(R.id.btnLogMain);
        Login.setOnClickListener(buttonLoginOnClickListener);
        NewUser = (Button)findViewById(R.id.btnNewUser);
        NewUser.setOnClickListener(buttonNewUserOnClickListener);

    }



    Button.OnClickListener buttonLoginOnClickListener
       = new Button.OnClickListener(){

        @Override
          public void onClick(View arg0) {
            //SQLiteAdapter db = new SQLiteAdapter (LoginActivity.this );
            SQLiteDatabase db = new SQLiteAdapter(LoginActivity.this).openToWrite();
            String email = EmailAdd.getText().toString();
             String password = Password.getText().toString();

            Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= ? AND password=?", new String[] {email, password});
            if(c.moveToFirst()) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                Intent main = new Intent(LoginActivity.this, MainMenuActivity.class);
                startActivity(main);
            } else {
                Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
            }
            Intent register = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(register);
    }
    };
    /*@Override
    public void onClick(View arg0) {
        Toast.makeText(getApplicationContext(), "Working to here!", Toast.LENGTH_SHORT).show();
         String data1 = EmailAdd.getText().toString();
         String data2 = Password.getText().toString();

        boolean invalid = false;
        //uname == null || uname.length() == 0
        if(data1.equals("")){
            invalid = true;
            Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
        }else if(data2.equals("")){
            invalid = true;
            Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
        }

        if(invalid == false){
            //need to check here for valid email and password

            Intent i_register = new Intent(LoginActivity.this, MainMenuActivity.class);
            startActivity(i_register);
            EmailAdd.setText("");
            Password.setText("");
            finish();
        }
        /*switch(v.getId()){

        case R.id.btnLogMain:
            mEmailAdd = (EditText)findViewById(R.id.email);
            mPassword = (EditText)findViewById(R.id.password);

            String uname = mEmailAdd.getText().toString();
            String pass = mPassword.getText().toString();

            if(uname.equals("") || uname == null){
                Toast.makeText(getApplicationContext(), "email Empty", Toast.LENGTH_SHORT).show();
            }else if(pass.equals("") || pass == null){
                Toast.makeText(getApplicationContext(), "Password Empty", Toast.LENGTH_SHORT).show();
            }else{
                boolean validLogin = validateLogin(uname, pass, LoginActivity.this);
                if(validLogin){
                    System.out.println("In Valid");
                    Intent i = new Intent(LoginActivity.this, MainMenuActivity.class);
                    startActivity(i);
                    finish();
                }
            }
            break;


        }*/
   // }
    //};

*RegisterActivity class*
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
//import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {

 EditText inputName, inputEmail, inputPassword;
 Button buttonRegister, buttonDeleteAll;
 TextView login;
 private SQLiteAdapter mySQLiteAdapter;
 ListView listContent;

 SimpleCursorAdapter cursorAdapter;
 Cursor cursor;


   /** Called when the activity is first created. */

@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.register_activity);


       inputName = (EditText)findViewById(R.id.name);
       inputEmail = (EditText)findViewById(R.id.email);
       inputPassword = (EditText)findViewById(R.id.password);
       buttonRegister = (Button)findViewById(R.id.register);
       //buttonDeleteAll = (Button)findViewById(R.id.showAll);

       listContent = (ListView)findViewById(R.id.contentlist);

       mySQLiteAdapter = new SQLiteAdapter(this);
       mySQLiteAdapter.openToWrite();

       cursor = mySQLiteAdapter.queueAll();
      // String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_NAME, SQLiteAdapter.KEY_EMAIL,SQLiteAdapter.KEY_PASSWORD};
       /*int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3};
       cursorAdapter =
        new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
       listContent.setAdapter(cursorAdapter);*/

       buttonRegister.setOnClickListener(buttonAddOnClickListener);
       //buttonShowAll.setOnClickListener(buttonShowAllOnClickListener);
       //addListenerOnRegButton();

       TextView loginScreen = (TextView) findViewById(R.id.login);

       // Listening to login 
       loginScreen.setOnClickListener(TextLoginOnClickListener);
}

TextView.OnClickListener TextLoginOnClickListener
= new TextView.OnClickListener(){



@Override
public void onClick(View arg0) {
    Intent i = new Intent(getApplicationContext(), LoginActivity.class);
    startActivity(i);

}
};

   Button.OnClickListener buttonAddOnClickListener
   = new Button.OnClickListener(){



  @Override
  public void onClick(View arg0) {



   String data1 = inputName.getText().toString();
   String data2 = inputEmail.getText().toString();
   String data3 = inputPassword.getText().toString();

   boolean invalid = false;

    if(data1.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Name Missing", Toast.LENGTH_SHORT).show();
    }else if(data2.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
    }else if(data3.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
    }

    if(invalid == false){
        mySQLiteAdapter.insert(data1, data2, data3);
        updateList();
           Toast.makeText(RegisterActivity.this, "You are now registered",
                   Toast.LENGTH_SHORT).show();

        Intent i_register = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(i_register);

        inputName.setText("");
        inputEmail.setText("");
        inputPassword.setText("");
        finish();
    }   




  }

   };




  /*Button.OnClickListener buttonShowAllOnClickListener1
   = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
      Intent i = new Intent(RegisterActivity.this, MainMenuActivity.class);
        startActivity(i);
        finish();
  }

   };

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  mySQLiteAdapter.close();
 }*/



 private void updateList(){
  //cursor.requery();
   }

}
    /* public boolean validateLogin(String uemail, String pass, Context context) {

        mydb = new SQLiteAdapter(this);
        SQLiteDatabase db = mydb.openToWrite();
        //SELECT
        String[] columns = {"_id"};

        //WHERE clause
        String selection = "email=? AND password=?";

        //WHERE clause arguments
        String[] selectionArgs = {uemail,pass};

        Cursor cursor = null;
        try{
        //SELECT _id FROM login WHERE email=uemail AND password=pass
        cursor = db.query(SQLiteAdapter.MYDATABASE_TABLE, columns, selection, selectionArgs, null, null, null,null);

        //  startManagingCursor(cursor);

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

        if(numberOfRows <= 0){

            Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
            return false;
        }


        return true;
    }*/



    Button.OnClickListener buttonNewUserOnClickListener
       = new Button.OnClickListener(){
        @Override
          public void onClick(View arg0) {

    }
    };



    }

**SQLiteAdapter **

package com.example.finalproject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLiteAdapter {

 public static final String MYDATABASE_NAME = "MY_PROJECT_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_USERS_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String KEY_NAME = "name";
 public static final String KEY_EMAIL = "email";
 public static final String KEY_PASSWORD = "password";

 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + MYDATABASE_TABLE + " ("
  + KEY_ID + " integer primary key autoincrement, "
  + KEY_NAME + " text not null, "
  + KEY_EMAIL + " text not null, "
  + KEY_PASSWORD + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 public SQLiteAdapter(Context c){
  context = c;
 }

 public SQLiteAdapter openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this; 
 }

 public SQLiteDatabase openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return sqLiteDatabase;
    }
 /*public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this; 
 }*/

 public void close(){
  sqLiteHelper.close();
 }

 public long insert(String name, String email, String password){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_NAME, name);
  contentValues.put(KEY_EMAIL, email);
  contentValues.put(KEY_PASSWORD, password);
  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

 public int deleteAll(){
  return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
 }

 public Cursor queueAll(){
  String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_EMAIL,KEY_PASSWORD};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
    null, null, null, null, null);

  return cursor;
 }

 public class SQLiteHelper extends SQLiteOpenHelper {

  public SQLiteHelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(SCRIPT_CREATE_DATABASE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
  }
 } 
}

我已经尝试了很多事情,但是当我点击登录按钮时,会出现一条消息“不幸停止工作”,提前感谢您的帮助,我们将不胜感激

日志猫

The application may be doing too much work on its main thread.
    02-04 00:49:58.305: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.305: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.415: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.415: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.874: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.884: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.894: I/Choreographer(7726): Skipped 73 frames!  The application may be doing too much work on its main thread.
    02-04 00:49:58.894: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.894: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.945: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.975: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:58.975: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:59.024: W/Trace(7726): Unexpected value from nativeGetEnabledTags: 0
    02-04 00:49:59.104: D/AndroidRuntime(7726): Shutting down VM
    02-04 00:49:59.104: W/dalvikvm(7726): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
    02-04 00:49:59.154: E/AndroidRuntime(7726): FATAL EXCEPTION: main
    02-04 00:49:59.154: E/AndroidRuntime(7726): java.lang.NullPointerException
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at com.example.finalproject.LoginActivity$2.onClick(LoginActivity.java:165)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.view.View.performClick(View.java:4202)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.view.View$PerformClick.run(View.java:17340)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.os.Handler.handleCallback(Handler.java:725)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.os.Handler.dispatchMessage(Handler.java:92)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.os.Looper.loop(Looper.java:137)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at java.lang.reflect.Method.invoke(Method.java:511)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    02-04 00:49:59.154: E/AndroidRuntime(7726):     at dalvik.system.NativeStart.main(Native Method)
    02-04 00:50:07.004: I/Process(7726): Sending signal. PID: 7726 SIG: 9

此代码仍然无法正常工作,请有人帮助我!

4

4 回答 4

1

use this query

SELECT email FROM MY_USERS_TABLE WHERE email=? AND password=?

you are using '?' use ? only

于 2013-02-14T17:57:59.327 回答
1

dbNULL并且你在下一行使用!

  SQLiteDatabase db = null;

  Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email='?' AND password='?'", new String[] {email, password});

通过调用修复它SQLiteDatabase db = new SQLiteAdapter(LoginActivity.this).openToWrite();

于 2013-02-14T17:55:29.273 回答
0

我相信你有几个问题。

  • 您正在将电子邮件设置为登录活动中的密码EmailAdd = (EditText)findViewById(R.id.password);
  • startActivity当您应该使用finish返回到调用活动时,您正在使用从活动返回。

以下是预注册用户和密码(Fred@email.com 以密码为密码)的改编(少注册)。

主菜单活动:-

public class MainMenuActivity extends AppCompatActivity {
    static boolean loggedin = false;
    SQLiteAdapter mAdapter;
    int requestcode = 100;
    Button menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        menu = (Button) this.findViewById(R.id.mymenu);

        mAdapter = new SQLiteAdapter(this);
        mAdapter.insert("Fred","Fred@email.com","password");
        if (!loggedin) {
            Intent intent = new Intent(this,LoginActivity.class);
            startActivityForResult(intent,requestcode);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    protected void onActivityResult(int reqcode, int reqresult, Intent intent) {
        if (reqcode == requestcode && reqresult == Activity.RESULT_OK) {
            loggedin = true;
            menu.setVisibility(View.VISIBLE);

        } else {
            loggedin = false;
            Toast.makeText(this,"You are now Logged In",Toast.LENGTH_SHORT).show();
        }
    }
}

activty_main_menu MainMenuActivity 使用的布局:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="so14881016login.so14881016login.MainMenuActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/mymenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:text="Menu"/>

</android.support.constraint.ConstraintLayout>

登录活动:-

public class LoginActivity extends Activity {

    EditText EmailAdd;
    EditText Password;
    Button Login;
    Button NewUser;
    private SQLiteAdapter db;
    int attempts = 3;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        //addListenerOnButton();

        //Button mNewUser = (Button)findViewById(R.id.btnLogMain);
        //mNewUser.setOnClickListener(this);

        EmailAdd = (EditText) findViewById(R.id.email);
        Password = (EditText) findViewById(R.id.password);

        Login = (Button) findViewById(R.id.btnLogMain);
        Login.setOnClickListener(buttonLoginOnClickListener);
        NewUser = (Button) findViewById(R.id.btnNewUser);
        //NewUser.setOnClickListener(buttonNewUserOnClickListener); //<<<<IGNORED
    }

    Button.OnClickListener buttonLoginOnClickListener
            = new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //SQLiteAdapter db = new SQLiteAdapter (LoginActivity.this );
            SQLiteDatabase db = new SQLiteAdapter(LoginActivity.this).openToWrite();
            String email = EmailAdd.getText().toString();
            String password = Password.getText().toString();

            //Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= ? AND password=?", new String[]{email, password});
            Cursor c = db.query(SQLiteAdapter.MYDATABASE_TABLE,
                    new String[]{SQLiteAdapter.KEY_EMAIL},
                    SQLiteAdapter.KEY_EMAIL + "=? AND " +SQLiteAdapter.KEY_PASSWORD + "=?",
                    new String[]{email,password},
                    null,null,null
            );
            if (c.moveToFirst()) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                // NO
                //Intent main = new Intent(LoginActivity.this, MainMenuActivity.class);
                //startActivity(main);
                setResult(Activity.RESULT_OK);
                finish(); //<<<< go back
            } else {
                Toast.makeText(getApplicationContext(), "Failed..\nTry Again", Toast.LENGTH_SHORT).show();
                if (--attempts <= 0) {
                    setResult(Activity.RESULT_CANCELED);
                    finish();
                }
            }
        }
    };
}

上面使用的login_activity布局:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btnLogMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"/>
    <Button
        android:id="@+id/btnNewUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add User"/>
</LinearLayout>

SQLite适配器

public class SQLiteAdapter {

    public static final String MYDATABASE_NAME = "MY_PROJECT_DATABASE";
    public static final String MYDATABASE_TABLE = "MY_USERS_TABLE";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_ID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_PASSWORD = "password";

    //create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE =
            "create table " + MYDATABASE_TABLE + " ("
                    + KEY_ID + " integer primary key autoincrement, "
                    + KEY_NAME + " text not null, "
                    + KEY_EMAIL + " text not null, "
                    + KEY_PASSWORD + " text not null);";

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c){
        context = c;
        openToWrite(); //<<<< Added
    }

    public SQLiteAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;
    }

    public SQLiteDatabase openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return sqLiteDatabase;
    }
 /*public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this;
 }*/

    public void close(){
        sqLiteHelper.close();
    }

    public long insert(String name, String email, String password){

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NAME, name);
        contentValues.put(KEY_EMAIL, email);
        contentValues.put(KEY_PASSWORD, password);
        return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
    }

    public int deleteAll(){
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }

    public Cursor queueAll(){
        String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_EMAIL,KEY_PASSWORD};
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
                null, null, null, null, null);

        return cursor;
    }

    public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                            SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_DATABASE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
        }
    }
}

笔记

  • 已包含一些评论。
  • 请注意结果(OK 或 NOT OK)是如何startActivityForResult.
  • 当调用/返回RegisterActivity时,应该应用相同的概念。

结果

首次运行时立即跳到登录活动:-

在此处输入图像描述

如果第 3 次返回MainMenuActivity且菜单按钮不可见,则无效登录尝试停留在上述允许重试(最多 3 次)。

有效的登录尝试返回MainMenuActivity使按钮(您的菜单等在此处)可见(什么都不做):-

在此处输入图像描述

于 2018-03-19T06:43:44.133 回答
0

我不确定,但你可以试试这个。

Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= '?' AND password='?'", new String[] {email, password});

或者

Cursor c = db.rawQuery("SELECT email FROM MY_USERS_TABLE WHERE email= '+email+' AND password='+password+'";

如果我错了,请原谅我。

于 2016-05-25T10:58:46.853 回答