2016-03-07 68 views
-1

我想打印整個數據庫數據,以便檢查我的SQLite代碼是否正常工作。
我取出由login.java用戶登錄信息,並希望表明,在loggedin.java如何顯示數據庫數據並在另一活動中顯示登錄詳細信息?

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://funnytadka.com:8060") 
       .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; 
    }} 

回答

0

編寫一個方法從數據庫表中檢索數據在

databasehandler.java

public Cursor getUserDetails() throws SQLException { 
     logger.debug("MYDATABASE_TABLE :- "+MYDATABASE_TABLE); 
     String q = "SELECT * FROM "+MYDATABASE_TABLE+" ;"; 
     logger.debug("query :- "+q); 
     Cursor mCursor = db.rawQuery(q, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     db.close(); 
     return mCursor; 
    } 
    public DBTariffHandler openToRead() throws android.database.SQLException { 
     sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, 
       MYDATABASE_VERSION); 
     db = sqLiteHelper.getReadableDatabase(); 
     return this; 
    } 

loggedin.java

databasehandler handler = new databasehandler(context); 
    handler.openToRead(); 
    Cursor cursor_next = handler.getUserDetails(); 
    handler.close() ; 

    if(cursor_next != null && cursor_next.moveToFirst()) 
    { 
     cursor_next.getColumnIndex(databasehandler.KEY_USERNAME) 
     //..ETC 
    } 
+0

線 logger.debug是給錯誤,記錄器不intialised –

+0

@scarletwing代替logger.debug();你也可以使用system.out.println(); – Gmaster

+0

sqLiteHelper = new SQLiteHelper(context,「logindb」,null,DATABASE_VERSION); db = sqLiteHelper.getReadableDatabase(); sqlitehelper也顯示錯誤,未初始化 –

相關問題