lundi 7 mars 2016

How can i show the database data and show the login details in another Activity?

I want to print the whole database data so that I can check if my SQLite code is working or not.
I am fetching the user login details from login.java and want to show that at loggedin.java

any help would be appreciated.

login.java

public class login extends AppCompatActivity {
    EditText e1, e2;
    ImageView i1, i2;
    Retrofit retrofit;
    TextView t;


    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        e1 = (EditText) findViewById(R.id.editText);
        if( e1.getText().toString().length() == 0 ){
            e1.setError( "email  is required" );}
        e2 = (EditText) findViewById(R.id.editText2);
        if( e2.getText().toString().length() == 0 ){
            e2.setError( "password is required" );}
        i1 = (ImageView) findViewById(R.id.email);
        i2 = (ImageView) findViewById(R.id.password);
        Button b1= (Button) findViewById(R.id.button);
        t=(TextView)findViewById(R.id.textView5);

        retrofit = new Retrofit.Builder()
                .baseUrl("http://ift.tt/1QZoRLv")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        b1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                db.insertEntry(e1.getText().toString(),e2.getText().toString());

            loginApi thisapi = retrofit.create(loginApi.class);
            Call<Pvideos> call = thisapi.loadData(e1.getText().toString(), e2.getText().toString());
            call.enqueue(new Callback<Pvideos>() {
                @Override
                public void onResponse(Response<Pvideos> response, Retrofit retrofit) {

                    if (response.isSuccess())
                    {



                        Intent intent = new Intent(login.this, loggedin.class);

                        startActivity(intent);
                    }

                    else

                    {
                        Toast.makeText(getApplicationContext(),"login unsuccessful",Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    Toast.makeText(getApplicationContext(),"login unsuccessful",Toast.LENGTH_SHORT).show();

                }




            });
            }
        });
        }

}

loggedin.java

    public class loggedin extends AppCompatActivity
    {
        View view;
       // public static final String DEFAULT = "N/A";
        TextView t1, t2;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.accinfo);
            t1 = (TextView) findViewById(R.id.textView3);
            t2 = (TextView) findViewById(R.id.textView4);
    }

databasehandler.java

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 2;
    private static final String DICTIONARY_TABLE_NAME = "logindb";
    static SQLiteDatabase db;
    private static final String DICTIONARY_TABLE_CREATE =
            "CREATE TABLE " + DICTIONARY_TABLE_NAME + "(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,email text,password text);";

    DatabaseHandler(Context context) {
        super(context, "logindb", null, DATABASE_VERSION);
        db = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DICTIONARY_TABLE_CREATE);
        db = this.db;

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }

    public void closeDb() {
        db.close();
        ;
    }

    public static boolean insertEntry(String email, String password) {
        ContentValues cv = new ContentValues();
        cv.put("email", email);
        cv.put("password", password);
        Log.e("email", email);
        Log.e("pass", password);
        long rowsEffected = db.insert(DICTIONARY_TABLE_NAME, null, cv);
        return rowsEffected > 0 ? true : false;
    }}

Aucun commentaire:

Enregistrer un commentaire