如何显示数据库数据并在另一个Activity中显示登录详细信息?

时间:2016-03-07 10:10:23

标签: java android database sqlite android-sqlite

我想打印整个数据库数据,以便检查我的SQLite代码是否正常工作 我从login.java获取用户登录详细信息,并希望在loggedin.java

中显示该信息

login.java

<Ellipse x:Name="ellipse" Fill="{TemplateBinding Background}" />

loggedin.java

 <Button x:Name="toggle"
    Width="132"
    Height="132"
    Margin="40,146,0,0"
    HorizontalAlignment="Left"
    VerticalAlignment="Top"
    Background="{Binding ElementName=MainControl,
                         Path=ButtonColor}"
    Style="{StaticResource MyButtonStyle}" />

databasehandler.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();

                }




            });
            }
        });
        }

}

1 个答案:

答案 0 :(得分: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
    }