How to retrieve a data from SQLite and view it in a TextView?











up vote
0
down vote

favorite












I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.



DataBaseHelper.java



public class DataBaseHelper extends SQLiteOpenHelper
{
private DataBaseHelper dbHelper;
public static SQLiteDatabase m_db;
public static final String DB_NAME = "users.dbHelper";
public static final int DB_VERSION = 1;
public static final String TABLE_USERS = "users";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String ALL_COLUMNS = {COLUMN_EMAIL, COLUMN_PASSWORD};
public static final String SQL_CREATE =
"CREATE TABLE IF NOT EXISTS " + TABLE_USERS + " (" +
COLUMN_EMAIL + " STRING PRIMARY KEY, " +
COLUMN_PASSWORD + " STRING);";
public static final String SQL_DROP = "DROP TABLE " + TABLE_USERS;
public DataBaseHelper(@Nullable Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(SQL_DROP);
onCreate(db);
}

//---opens the database---
public DataBaseHelper open() throws SQLException
{
m_db = dbHelper.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
if (m_db != null)
m_db.close();
if (dbHelper != null)
dbHelper.close();
}

// Inserting in database
public boolean insert(String email, String password)
{
m_db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email", email);
contentValues.put("password", password);
long ins = m_db.insert(TABLE_USERS, null, contentValues);
if (ins == -1) return false;
else return true;
}

// Checking if email exists
public boolean checkEmail(String COLUMN_EMAIL)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?",
new String{COLUMN_EMAIL});
if (cursor.getCount() > 0) return false;
else return true;
}

// Checking the email and password
public boolean checkEmailPassword(String COLUMN_EMAIL, String COLUMN_PASSWORD)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?
and COLUMN_PASSWORD=?", new String{COLUMN_EMAIL, COLUMN_PASSWORD});
if (cursor.getCount() > 0) return true;
else return false;
}

public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst();
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}
}


WelcomeActivity.Java



public class WelcomeActivity extends AppCompatActivity
{
private static int SPLASH_TIME_OUT = 4000;
private DataBaseHelper db;
private SQLiteDatabase m_db;
private TextView tvEmail2;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

db = new DataBaseHelper(this);

TextView tvEmail = findViewById(R.id.tvEmail2);
String email = db.getEmail(DataBaseHelper.COLUMN_EMAIL);
String user = email;

tvEmail.setText(Users.class.getEm);

new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(WelcomeActivity.this, EthicsActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}


Users.java



public class Users
{
private String email;
private String password;
public Users() {}

public Users(String email, String password)
{
this.email = email;
this.password = password;
}

public String getEmail()
{
return email;
}

public void setEmail(String email)
{
this.email = email;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}

@Override
public String toString()
{
return "Users{" +
"email='" + email + ''' +
", password='" + password + ''' +
'}';
}
}


What do you guys think about this?
I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.










share|improve this question









New contributor




Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Can you please let me know the logcat?
    – Ümañg ßürmån
    Nov 10 at 15:12










  • what is the error you are getting?
    – ManishPrajapati
    Nov 10 at 15:12










  • idk if its an error now, the activity is closing and does not move to a new activity as intented
    – Spectra
    Nov 10 at 15:27










  • Post the logcat
    – shashank chandak
    Nov 10 at 17:39















up vote
0
down vote

favorite












I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.



DataBaseHelper.java



public class DataBaseHelper extends SQLiteOpenHelper
{
private DataBaseHelper dbHelper;
public static SQLiteDatabase m_db;
public static final String DB_NAME = "users.dbHelper";
public static final int DB_VERSION = 1;
public static final String TABLE_USERS = "users";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String ALL_COLUMNS = {COLUMN_EMAIL, COLUMN_PASSWORD};
public static final String SQL_CREATE =
"CREATE TABLE IF NOT EXISTS " + TABLE_USERS + " (" +
COLUMN_EMAIL + " STRING PRIMARY KEY, " +
COLUMN_PASSWORD + " STRING);";
public static final String SQL_DROP = "DROP TABLE " + TABLE_USERS;
public DataBaseHelper(@Nullable Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(SQL_DROP);
onCreate(db);
}

//---opens the database---
public DataBaseHelper open() throws SQLException
{
m_db = dbHelper.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
if (m_db != null)
m_db.close();
if (dbHelper != null)
dbHelper.close();
}

// Inserting in database
public boolean insert(String email, String password)
{
m_db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email", email);
contentValues.put("password", password);
long ins = m_db.insert(TABLE_USERS, null, contentValues);
if (ins == -1) return false;
else return true;
}

// Checking if email exists
public boolean checkEmail(String COLUMN_EMAIL)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?",
new String{COLUMN_EMAIL});
if (cursor.getCount() > 0) return false;
else return true;
}

// Checking the email and password
public boolean checkEmailPassword(String COLUMN_EMAIL, String COLUMN_PASSWORD)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?
and COLUMN_PASSWORD=?", new String{COLUMN_EMAIL, COLUMN_PASSWORD});
if (cursor.getCount() > 0) return true;
else return false;
}

public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst();
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}
}


WelcomeActivity.Java



public class WelcomeActivity extends AppCompatActivity
{
private static int SPLASH_TIME_OUT = 4000;
private DataBaseHelper db;
private SQLiteDatabase m_db;
private TextView tvEmail2;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

db = new DataBaseHelper(this);

TextView tvEmail = findViewById(R.id.tvEmail2);
String email = db.getEmail(DataBaseHelper.COLUMN_EMAIL);
String user = email;

tvEmail.setText(Users.class.getEm);

new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(WelcomeActivity.this, EthicsActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}


Users.java



public class Users
{
private String email;
private String password;
public Users() {}

public Users(String email, String password)
{
this.email = email;
this.password = password;
}

public String getEmail()
{
return email;
}

public void setEmail(String email)
{
this.email = email;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}

@Override
public String toString()
{
return "Users{" +
"email='" + email + ''' +
", password='" + password + ''' +
'}';
}
}


What do you guys think about this?
I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.










share|improve this question









New contributor




Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Can you please let me know the logcat?
    – Ümañg ßürmån
    Nov 10 at 15:12










  • what is the error you are getting?
    – ManishPrajapati
    Nov 10 at 15:12










  • idk if its an error now, the activity is closing and does not move to a new activity as intented
    – Spectra
    Nov 10 at 15:27










  • Post the logcat
    – shashank chandak
    Nov 10 at 17:39













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.



DataBaseHelper.java



public class DataBaseHelper extends SQLiteOpenHelper
{
private DataBaseHelper dbHelper;
public static SQLiteDatabase m_db;
public static final String DB_NAME = "users.dbHelper";
public static final int DB_VERSION = 1;
public static final String TABLE_USERS = "users";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String ALL_COLUMNS = {COLUMN_EMAIL, COLUMN_PASSWORD};
public static final String SQL_CREATE =
"CREATE TABLE IF NOT EXISTS " + TABLE_USERS + " (" +
COLUMN_EMAIL + " STRING PRIMARY KEY, " +
COLUMN_PASSWORD + " STRING);";
public static final String SQL_DROP = "DROP TABLE " + TABLE_USERS;
public DataBaseHelper(@Nullable Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(SQL_DROP);
onCreate(db);
}

//---opens the database---
public DataBaseHelper open() throws SQLException
{
m_db = dbHelper.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
if (m_db != null)
m_db.close();
if (dbHelper != null)
dbHelper.close();
}

// Inserting in database
public boolean insert(String email, String password)
{
m_db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email", email);
contentValues.put("password", password);
long ins = m_db.insert(TABLE_USERS, null, contentValues);
if (ins == -1) return false;
else return true;
}

// Checking if email exists
public boolean checkEmail(String COLUMN_EMAIL)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?",
new String{COLUMN_EMAIL});
if (cursor.getCount() > 0) return false;
else return true;
}

// Checking the email and password
public boolean checkEmailPassword(String COLUMN_EMAIL, String COLUMN_PASSWORD)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?
and COLUMN_PASSWORD=?", new String{COLUMN_EMAIL, COLUMN_PASSWORD});
if (cursor.getCount() > 0) return true;
else return false;
}

public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst();
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}
}


WelcomeActivity.Java



public class WelcomeActivity extends AppCompatActivity
{
private static int SPLASH_TIME_OUT = 4000;
private DataBaseHelper db;
private SQLiteDatabase m_db;
private TextView tvEmail2;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

db = new DataBaseHelper(this);

TextView tvEmail = findViewById(R.id.tvEmail2);
String email = db.getEmail(DataBaseHelper.COLUMN_EMAIL);
String user = email;

tvEmail.setText(Users.class.getEm);

new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(WelcomeActivity.this, EthicsActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}


Users.java



public class Users
{
private String email;
private String password;
public Users() {}

public Users(String email, String password)
{
this.email = email;
this.password = password;
}

public String getEmail()
{
return email;
}

public void setEmail(String email)
{
this.email = email;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}

@Override
public String toString()
{
return "Users{" +
"email='" + email + ''' +
", password='" + password + ''' +
'}';
}
}


What do you guys think about this?
I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.










share|improve this question









New contributor




Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.



DataBaseHelper.java



public class DataBaseHelper extends SQLiteOpenHelper
{
private DataBaseHelper dbHelper;
public static SQLiteDatabase m_db;
public static final String DB_NAME = "users.dbHelper";
public static final int DB_VERSION = 1;
public static final String TABLE_USERS = "users";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PASSWORD = "password";
public static final String ALL_COLUMNS = {COLUMN_EMAIL, COLUMN_PASSWORD};
public static final String SQL_CREATE =
"CREATE TABLE IF NOT EXISTS " + TABLE_USERS + " (" +
COLUMN_EMAIL + " STRING PRIMARY KEY, " +
COLUMN_PASSWORD + " STRING);";
public static final String SQL_DROP = "DROP TABLE " + TABLE_USERS;
public DataBaseHelper(@Nullable Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL(SQL_DROP);
onCreate(db);
}

//---opens the database---
public DataBaseHelper open() throws SQLException
{
m_db = dbHelper.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
if (m_db != null)
m_db.close();
if (dbHelper != null)
dbHelper.close();
}

// Inserting in database
public boolean insert(String email, String password)
{
m_db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("email", email);
contentValues.put("password", password);
long ins = m_db.insert(TABLE_USERS, null, contentValues);
if (ins == -1) return false;
else return true;
}

// Checking if email exists
public boolean checkEmail(String COLUMN_EMAIL)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?",
new String{COLUMN_EMAIL});
if (cursor.getCount() > 0) return false;
else return true;
}

// Checking the email and password
public boolean checkEmailPassword(String COLUMN_EMAIL, String COLUMN_PASSWORD)
{
m_db = this.getWritableDatabase();
Cursor cursor = m_db.rawQuery("select * from TABLE_USERS where COLUMN_EMAIL=?
and COLUMN_PASSWORD=?", new String{COLUMN_EMAIL, COLUMN_PASSWORD});
if (cursor.getCount() > 0) return true;
else return false;
}

public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst();
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}
}


WelcomeActivity.Java



public class WelcomeActivity extends AppCompatActivity
{
private static int SPLASH_TIME_OUT = 4000;
private DataBaseHelper db;
private SQLiteDatabase m_db;
private TextView tvEmail2;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

db = new DataBaseHelper(this);

TextView tvEmail = findViewById(R.id.tvEmail2);
String email = db.getEmail(DataBaseHelper.COLUMN_EMAIL);
String user = email;

tvEmail.setText(Users.class.getEm);

new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Intent intent = new Intent(WelcomeActivity.this, EthicsActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_TIME_OUT);
}
}


Users.java



public class Users
{
private String email;
private String password;
public Users() {}

public Users(String email, String password)
{
this.email = email;
this.password = password;
}

public String getEmail()
{
return email;
}

public void setEmail(String email)
{
this.email = email;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}

@Override
public String toString()
{
return "Users{" +
"email='" + email + ''' +
", password='" + password + ''' +
'}';
}
}


What do you guys think about this?
I am trying to retrieve my email from my SQLiteDbHelper and view it in a TextView in another activity after I click sign in, using the WelcomeActivity or splash activity since I only want it to be view for 4 seconds, but idk whats the problem when I sign in it crashes me out.







java android database xcode sqlite






share|improve this question









New contributor




Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 19:34









MikeT

13.1k102440




13.1k102440






New contributor




Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 14:58









Spectra

54




54




New contributor




Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Spectra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Can you please let me know the logcat?
    – Ümañg ßürmån
    Nov 10 at 15:12










  • what is the error you are getting?
    – ManishPrajapati
    Nov 10 at 15:12










  • idk if its an error now, the activity is closing and does not move to a new activity as intented
    – Spectra
    Nov 10 at 15:27










  • Post the logcat
    – shashank chandak
    Nov 10 at 17:39


















  • Can you please let me know the logcat?
    – Ümañg ßürmån
    Nov 10 at 15:12










  • what is the error you are getting?
    – ManishPrajapati
    Nov 10 at 15:12










  • idk if its an error now, the activity is closing and does not move to a new activity as intented
    – Spectra
    Nov 10 at 15:27










  • Post the logcat
    – shashank chandak
    Nov 10 at 17:39
















Can you please let me know the logcat?
– Ümañg ßürmån
Nov 10 at 15:12




Can you please let me know the logcat?
– Ümañg ßürmån
Nov 10 at 15:12












what is the error you are getting?
– ManishPrajapati
Nov 10 at 15:12




what is the error you are getting?
– ManishPrajapati
Nov 10 at 15:12












idk if its an error now, the activity is closing and does not move to a new activity as intented
– Spectra
Nov 10 at 15:27




idk if its an error now, the activity is closing and does not move to a new activity as intented
– Spectra
Nov 10 at 15:27












Post the logcat
– shashank chandak
Nov 10 at 17:39




Post the logcat
– shashank chandak
Nov 10 at 17:39












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I believe that you have a number of issues.



The line tvEmail.setText(Users.class.getEm); won't compilem I believe that instead want tvEmail.setText(user); or tvEmail.setText(email); (you don't need to have both email and user Strings having the same value).



Assuming that EthicsActivity is a valid working activity, and that there is an appropriate activity entry in the manifest AndroidManifest.xml then the issue is that you would get an error along the lines of :-



11-10 19:56:12.863 1170-1170/so53240174.so53240174 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so53240174.so53240174/so53240174.so53240174.WelcomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)
at so53240174.so53240174.WelcomeActivity.onCreate(WelcomeActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 


This is saying that the Cursor is empty so you cannot get any data from it and it happens at the line String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)); which is in the getEmail method as indicated by the line in the log :-




at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)



The reason why this is happening is that a) there are no rows in the users table and b) that the Cursor moveToFirst method's result isn't checked. Instead an assumption is made that it will always move to the first row in the Cursor.



Instead of the getEmail method being :-



public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst(); //<<<<<<<<<< WRONG
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}


It could be :-



public String getEmail(String COLUMN_EMAIL) {
String user = "No Email Found."; //<<<<<<<<<< Default value in case of empty table
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
if (cursor.moveToFirst()) { //<<<<<<<<<< checks result of the move
user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
}
cursor.close();
return user;
}


Of course applying this fix, unless a row is added to the users table will always result in the TextView displaying No Email Found. If there are multiple rows then it will display the email from one of the rows, not necessarily a specific row.



Adding the line db.insert("fred@fredmail.com","1234567890"); before the value is retrieved using the call to the getEmail results in fred@fredmail.com being displayed in the TextView.






share|improve this answer





















  • That was helpful sir! but now i have stumbled into a new problem, it is as you said it will retrieve the last username entered, but that's not the case i want my text view in welcome activity capture the email that was entered in the sign in activity, i want to retrieve that for every user i am trying to enter, right now i can only retrieve the last registered user email no matter what other users i am signing in with, do you have an idea how to do that?
    – Spectra
    Nov 11 at 8:28










  • The way that StackOverflow is meant to work, is that you ask a question for each issue you are having, so what you ask should be raised as a new question.
    – MikeT
    Nov 11 at 19:21










  • I apologze sir!
    – Spectra
    Nov 12 at 16:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Spectra is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240174%2fhow-to-retrieve-a-data-from-sqlite-and-view-it-in-a-textview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I believe that you have a number of issues.



The line tvEmail.setText(Users.class.getEm); won't compilem I believe that instead want tvEmail.setText(user); or tvEmail.setText(email); (you don't need to have both email and user Strings having the same value).



Assuming that EthicsActivity is a valid working activity, and that there is an appropriate activity entry in the manifest AndroidManifest.xml then the issue is that you would get an error along the lines of :-



11-10 19:56:12.863 1170-1170/so53240174.so53240174 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so53240174.so53240174/so53240174.so53240174.WelcomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)
at so53240174.so53240174.WelcomeActivity.onCreate(WelcomeActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 


This is saying that the Cursor is empty so you cannot get any data from it and it happens at the line String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)); which is in the getEmail method as indicated by the line in the log :-




at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)



The reason why this is happening is that a) there are no rows in the users table and b) that the Cursor moveToFirst method's result isn't checked. Instead an assumption is made that it will always move to the first row in the Cursor.



Instead of the getEmail method being :-



public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst(); //<<<<<<<<<< WRONG
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}


It could be :-



public String getEmail(String COLUMN_EMAIL) {
String user = "No Email Found."; //<<<<<<<<<< Default value in case of empty table
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
if (cursor.moveToFirst()) { //<<<<<<<<<< checks result of the move
user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
}
cursor.close();
return user;
}


Of course applying this fix, unless a row is added to the users table will always result in the TextView displaying No Email Found. If there are multiple rows then it will display the email from one of the rows, not necessarily a specific row.



Adding the line db.insert("fred@fredmail.com","1234567890"); before the value is retrieved using the call to the getEmail results in fred@fredmail.com being displayed in the TextView.






share|improve this answer





















  • That was helpful sir! but now i have stumbled into a new problem, it is as you said it will retrieve the last username entered, but that's not the case i want my text view in welcome activity capture the email that was entered in the sign in activity, i want to retrieve that for every user i am trying to enter, right now i can only retrieve the last registered user email no matter what other users i am signing in with, do you have an idea how to do that?
    – Spectra
    Nov 11 at 8:28










  • The way that StackOverflow is meant to work, is that you ask a question for each issue you are having, so what you ask should be raised as a new question.
    – MikeT
    Nov 11 at 19:21










  • I apologze sir!
    – Spectra
    Nov 12 at 16:46















up vote
0
down vote



accepted










I believe that you have a number of issues.



The line tvEmail.setText(Users.class.getEm); won't compilem I believe that instead want tvEmail.setText(user); or tvEmail.setText(email); (you don't need to have both email and user Strings having the same value).



Assuming that EthicsActivity is a valid working activity, and that there is an appropriate activity entry in the manifest AndroidManifest.xml then the issue is that you would get an error along the lines of :-



11-10 19:56:12.863 1170-1170/so53240174.so53240174 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so53240174.so53240174/so53240174.so53240174.WelcomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)
at so53240174.so53240174.WelcomeActivity.onCreate(WelcomeActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 


This is saying that the Cursor is empty so you cannot get any data from it and it happens at the line String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)); which is in the getEmail method as indicated by the line in the log :-




at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)



The reason why this is happening is that a) there are no rows in the users table and b) that the Cursor moveToFirst method's result isn't checked. Instead an assumption is made that it will always move to the first row in the Cursor.



Instead of the getEmail method being :-



public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst(); //<<<<<<<<<< WRONG
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}


It could be :-



public String getEmail(String COLUMN_EMAIL) {
String user = "No Email Found."; //<<<<<<<<<< Default value in case of empty table
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
if (cursor.moveToFirst()) { //<<<<<<<<<< checks result of the move
user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
}
cursor.close();
return user;
}


Of course applying this fix, unless a row is added to the users table will always result in the TextView displaying No Email Found. If there are multiple rows then it will display the email from one of the rows, not necessarily a specific row.



Adding the line db.insert("fred@fredmail.com","1234567890"); before the value is retrieved using the call to the getEmail results in fred@fredmail.com being displayed in the TextView.






share|improve this answer





















  • That was helpful sir! but now i have stumbled into a new problem, it is as you said it will retrieve the last username entered, but that's not the case i want my text view in welcome activity capture the email that was entered in the sign in activity, i want to retrieve that for every user i am trying to enter, right now i can only retrieve the last registered user email no matter what other users i am signing in with, do you have an idea how to do that?
    – Spectra
    Nov 11 at 8:28










  • The way that StackOverflow is meant to work, is that you ask a question for each issue you are having, so what you ask should be raised as a new question.
    – MikeT
    Nov 11 at 19:21










  • I apologze sir!
    – Spectra
    Nov 12 at 16:46













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I believe that you have a number of issues.



The line tvEmail.setText(Users.class.getEm); won't compilem I believe that instead want tvEmail.setText(user); or tvEmail.setText(email); (you don't need to have both email and user Strings having the same value).



Assuming that EthicsActivity is a valid working activity, and that there is an appropriate activity entry in the manifest AndroidManifest.xml then the issue is that you would get an error along the lines of :-



11-10 19:56:12.863 1170-1170/so53240174.so53240174 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so53240174.so53240174/so53240174.so53240174.WelcomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)
at so53240174.so53240174.WelcomeActivity.onCreate(WelcomeActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 


This is saying that the Cursor is empty so you cannot get any data from it and it happens at the line String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)); which is in the getEmail method as indicated by the line in the log :-




at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)



The reason why this is happening is that a) there are no rows in the users table and b) that the Cursor moveToFirst method's result isn't checked. Instead an assumption is made that it will always move to the first row in the Cursor.



Instead of the getEmail method being :-



public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst(); //<<<<<<<<<< WRONG
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}


It could be :-



public String getEmail(String COLUMN_EMAIL) {
String user = "No Email Found."; //<<<<<<<<<< Default value in case of empty table
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
if (cursor.moveToFirst()) { //<<<<<<<<<< checks result of the move
user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
}
cursor.close();
return user;
}


Of course applying this fix, unless a row is added to the users table will always result in the TextView displaying No Email Found. If there are multiple rows then it will display the email from one of the rows, not necessarily a specific row.



Adding the line db.insert("fred@fredmail.com","1234567890"); before the value is retrieved using the call to the getEmail results in fred@fredmail.com being displayed in the TextView.






share|improve this answer












I believe that you have a number of issues.



The line tvEmail.setText(Users.class.getEm); won't compilem I believe that instead want tvEmail.setText(user); or tvEmail.setText(email); (you don't need to have both email and user Strings having the same value).



Assuming that EthicsActivity is a valid working activity, and that there is an appropriate activity entry in the manifest AndroidManifest.xml then the issue is that you would get an error along the lines of :-



11-10 19:56:12.863 1170-1170/so53240174.so53240174 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so53240174.so53240174/so53240174.so53240174.WelcomeActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)
at so53240174.so53240174.WelcomeActivity.onCreate(WelcomeActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 


This is saying that the Cursor is empty so you cannot get any data from it and it happens at the line String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL)); which is in the getEmail method as indicated by the line in the log :-




at so53240174.so53240174.DataBaseHelper.getEmail(DataBaseHelper.java:96)



The reason why this is happening is that a) there are no rows in the users table and b) that the Cursor moveToFirst method's result isn't checked. Instead an assumption is made that it will always move to the first row in the Cursor.



Instead of the getEmail method being :-



public String getEmail(String COLUMN_EMAIL)
{
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
cursor.moveToFirst(); //<<<<<<<<<< WRONG
String user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
cursor.close();
return user;
}


It could be :-



public String getEmail(String COLUMN_EMAIL) {
String user = "No Email Found."; //<<<<<<<<<< Default value in case of empty table
m_db = this.getReadableDatabase();
Cursor cursor = m_db.query(TABLE_USERS, new String{COLUMN_EMAIL}, null, null,
null, null, null);
if (cursor.moveToFirst()) { //<<<<<<<<<< checks result of the move
user = cursor.getString(cursor.getColumnIndex(COLUMN_EMAIL));
}
cursor.close();
return user;
}


Of course applying this fix, unless a row is added to the users table will always result in the TextView displaying No Email Found. If there are multiple rows then it will display the email from one of the rows, not necessarily a specific row.



Adding the line db.insert("fred@fredmail.com","1234567890"); before the value is retrieved using the call to the getEmail results in fred@fredmail.com being displayed in the TextView.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 20:39









MikeT

13.1k102440




13.1k102440












  • That was helpful sir! but now i have stumbled into a new problem, it is as you said it will retrieve the last username entered, but that's not the case i want my text view in welcome activity capture the email that was entered in the sign in activity, i want to retrieve that for every user i am trying to enter, right now i can only retrieve the last registered user email no matter what other users i am signing in with, do you have an idea how to do that?
    – Spectra
    Nov 11 at 8:28










  • The way that StackOverflow is meant to work, is that you ask a question for each issue you are having, so what you ask should be raised as a new question.
    – MikeT
    Nov 11 at 19:21










  • I apologze sir!
    – Spectra
    Nov 12 at 16:46


















  • That was helpful sir! but now i have stumbled into a new problem, it is as you said it will retrieve the last username entered, but that's not the case i want my text view in welcome activity capture the email that was entered in the sign in activity, i want to retrieve that for every user i am trying to enter, right now i can only retrieve the last registered user email no matter what other users i am signing in with, do you have an idea how to do that?
    – Spectra
    Nov 11 at 8:28










  • The way that StackOverflow is meant to work, is that you ask a question for each issue you are having, so what you ask should be raised as a new question.
    – MikeT
    Nov 11 at 19:21










  • I apologze sir!
    – Spectra
    Nov 12 at 16:46
















That was helpful sir! but now i have stumbled into a new problem, it is as you said it will retrieve the last username entered, but that's not the case i want my text view in welcome activity capture the email that was entered in the sign in activity, i want to retrieve that for every user i am trying to enter, right now i can only retrieve the last registered user email no matter what other users i am signing in with, do you have an idea how to do that?
– Spectra
Nov 11 at 8:28




That was helpful sir! but now i have stumbled into a new problem, it is as you said it will retrieve the last username entered, but that's not the case i want my text view in welcome activity capture the email that was entered in the sign in activity, i want to retrieve that for every user i am trying to enter, right now i can only retrieve the last registered user email no matter what other users i am signing in with, do you have an idea how to do that?
– Spectra
Nov 11 at 8:28












The way that StackOverflow is meant to work, is that you ask a question for each issue you are having, so what you ask should be raised as a new question.
– MikeT
Nov 11 at 19:21




The way that StackOverflow is meant to work, is that you ask a question for each issue you are having, so what you ask should be raised as a new question.
– MikeT
Nov 11 at 19:21












I apologze sir!
– Spectra
Nov 12 at 16:46




I apologze sir!
– Spectra
Nov 12 at 16:46










Spectra is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Spectra is a new contributor. Be nice, and check out our Code of Conduct.













Spectra is a new contributor. Be nice, and check out our Code of Conduct.












Spectra is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240174%2fhow-to-retrieve-a-data-from-sqlite-and-view-it-in-a-textview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values