SimpleCursor Adapter - Cannot use this in Static Context - java

Dear fellow senior programmers,
I encounter a runtime error of cannot use this in my databasehandler.java. Is there anywhere to overcome this problem.
Main Activity
public class DatabaseActivity extends Activity {
TextView idView;
EditText productBox;
EditText quantityBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);
idView = (TextView) findViewById(R.id.productID);
productBox = (EditText) findViewById(R.id.productName);
quantityBox = (EditText) findViewById(R.id.productQuantity);
ListView listContent = (ListView) findViewById(R.id.listView1);
Cursor cursor = MyDBHandler.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{MyDBHandler.COLUMN_PRODUCTNAME};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.rolypoly, cursor, from, to);
listContent.setAdapter(cursorAdapter);
}
MyDBHandler.java
public static Cursor queueAll(){
String[] columns = new String[]{COLUMN_ID, COLUMN_PRODUCTNAME,COLUMN_QUANTITY};
SQLiteDatabase db = **this**.getReadableDatabase();
Cursor cursor = db.query(TABLE_PRODUCTS, columns,
null, null, null, null, null);
}

Yes as error clearly says you cannot use this inside a static method. this refers to the "currently invoking" object, where as static is not tied with any object, you cannot use this in static context. Quote from Oracle tutorial:
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called
Instead of this:
this.getReadableDatabase();
You could do like this to access the getReadableDatabase();:
new YourClassName().getReadableDatabase();

Related

I Can't get my data because of Null Pointer Exception in android

When i try to get the data from my data base using my view data method, it's just gives me null pointer exception even if my data base have data, the view data method :
public String viewdata3(){
SQLiteDatabase sqLiteDatabase4 = dataBase.getWritableDatabase();
String [] classdetail = {DataBase.classname,DataBase.studentsnumber};
#SuppressLint("Recycle") Cursor cursor = sqLiteDatabase4.query(DataBase.tablename1,classdetail,null,
null,null,null,null,null);
StringBuilder stringBuilder = new StringBuilder();
while (cursor.moveToNext()){
String classnamee = cursor.getString(0);
int stdntnmbr = cursor.getInt(1);
stringBuilder.append(classnamee+" "+stdntnmbr+" "+"/n");
}
return viewdata3();
whenever i call this method the app just crashes and the same to any other method like this, the data restoration method :
DataBaseConnection db;
ArrayAdapter<String> adapter;
String [] data = {Objects.requireNonNull(db).viewdata3()};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
ListView classes = findViewById(R.id.ListView);
adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,data);
classes.setAdapter(adapter);
It seems that you want to read out of your database before you write data in it.
I would start with the android sqllite-example:
https://developer.android.com/reference/android/database/sqlite/package-summary
don't forget it's an in-memory database which probably means that you will loose your DB-Data when the app is closing.
I think the issue is here
String [] data = {Objects.requireNonNull(db).viewdata3()};
You should initialize the db before you call above line.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
db = .... // initialize your db;
data = {Objects.requireNonNull(db).viewdata3()};
....
}

Pass widget input to DatabaseHelperSource file

Disclaimer: I am a newbie to Android development :)
How can I pass the string values collected from this first class to the class below? I attempted this but only got null values.
Here's my main activity.
public class Register extends AppCompatActivity {
protected SnapToSellDataSource mDataSource;
public String sFullname;
public String sEmail;
public String sMobileNumber;
public String sPassword;
EditText full_name, email, mobile_number, pwd, copwd;
Button registerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mDataSource = new SnapToSellDataSource(Register.this);
full_name = (EditText) findViewById(R.id.editText);
email = (EditText) findViewById(R.id.editText2);
mobile_number = (EditText) findViewById(R.id.editText3);
pwd = (EditText) findViewById(R.id.editText4);
copwd = (EditText) findViewById(R.id.editText5);
registerButton = (Button) findViewById(R.id.button);
registerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Register register = new Register();
String editPassword = pwd.getText().toString();
String editConfirmPassword = copwd.getText().toString();
if(editPassword.equals(editConfirmPassword)) {
//This isn't overwriting the null class variables I
//instantiated so that I can pass them to the class below
sFullname = full_name.getText().toString();
sEmail = email.getText().toString();
sMobileNumber = mobile_number.getText().toString();
sPassword = pwd.getText().toString();
mDataSource.insertUser(register);
}
}
});
}
}
Here's the class that should receive the string values:
public class SnapToSellDataSource {
private SQLiteDatabase mDatabase;
private SnapToSellHelper mHelper;
private Context mContext;
public SnapToSellDataSource(Context context){
mContext = context;
mHelper = new SnapToSellHelper(mContext);
}
public void insertUser(Register register){
ContentValues values = new ContentValues();
values.put(SnapToSellHelper.COL_NAME, register.sFullname);
values.put(SnapToSellHelper.COL_EMAIL, register.sEmail);
values.put(SnapToSellHelper.COL_NUMBER, register.sMobileNumber);
values.put(SnapToSellHelper.COL_PASSWORD, register.sPassword);
mDatabase.insert(SnapToSellHelper.TBL_USERS, null, values);
}
}
I attempted to getText, getString from the second class but my app crashed maybe since the widgets were not yet assigned ids at the class level. Passing actual string values encased in quoation marks ("") works so it means the DatabaseHelper is properly set up.
I also tried declaring class variables and assigning the widget values to them but kept getting the "Cannot resolve symbol" error.
How you get a read from the local variables and pass them to the class variables that can then be set as public and read by another class; in this case, the second class?
You can not simply create instances of an activity in Android. Activities are not classes that you just do a “new” on and call their constructor. An instance of an Activity is created when the app starts or when an Intent starts an activity.
So doing this: Register register = new Register(); is not good! You can find good arguments here
Instead you can pass those values as parameters to the insertUser(params...) method or create a new User class and instantiate it with those string values and pass it to insertUser(user) method.
Method call:
mDataSource.insertUser(sFullname, sEmail, sMobileNumber, sPassword);
Method definition:
public void insertUser(String sFullname, String sEmail, String sMobileNumber, String sPassword) {
ContentValues values = new ContentValues();
values.put(SnapToSellHelper.COL_NAME, sFullname);
values.put(SnapToSellHelper.COL_EMAIL, sEmail);
values.put(SnapToSellHelper.COL_NUMBER, sMobileNumber);
values.put(SnapToSellHelper.COL_PASSWORD, sPassword);
mDatabase.insert(SnapToSellHelper.TBL_USERS, null, values);
}

How Do I get Data from SQLite DB to other activity which has ListView

I want to get the data from getName() method which is in Database class and put those data into ListView.Can anyone please help me out here. It crashes everytime I try to open this activity.
Caused by : java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.addAll(java.util.Collection)' on a null object reference"
This is the Error msg that appears when I run below code:
public class ListActivity extends android.app.ListActivity {
ListView mListNames;
ArrayList<String> mNames;
DBForm dbForm = new DBForm(this);`
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mListNames = (ListView) findViewById(android.R.id.list);
mNames.addAll(dbForm.getName());
this.setListAdapter(new ArrayAdapter<>(this, R.layout.list_head, mNames));
}
}
And this how I store and Retrieve the data inside DB class:
public ArrayList<String> getName() {
ArrayList<String> array_list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select name from contacts" , null);
res.moveToFirst();
while (!res.isAfterLast()) {
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
res.moveToNext();
}
res.close();
return array_list;
}
Your problem in getName() method. Maybe your table is empty. You need to check null of your cursor before loop it. Like below:
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
String name = cursor.getString(cursor.getColumnIndex(countyname));
list.add(name);
cursor.moveToNext();
}
}
Refer to: Get all rows from SQLite
So I made a silly mistake here in defining the target XML file.I choose one layout file and instead of choosing the textView id from the same layout file I accidently put another layout files textView id.
adapter = new ArrayAdapter<>(this, R.layout.list_view, R.id.list_names, mNames);
mListNames.setAdapter(adapter);

Using a cursor to return rows from SQLite, error when database is empty

When my database is empty, or it has just been created I am getting this error,
03-10 17:34:40.758: E/AndroidRuntime(1144):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.adressbooktake2/com.example.adressbooktake2.MainActivity}:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Here is my code in my main class,
public class MainActivity extends Activity {
DBAdaptor db;
Cursor cursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBAdaptor(this).open();
cursor = db.getAllRecords();
DisplayRecord(cursor);
}
which then calls this code in my DBAdaptor class,
public Cursor getAllRecords()
{
Cursor gaRecords = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_PHONENUMBER, KEY_EMAIL}, null, null, null, null, null);
gaRecords.moveToFirst();
return gaRecords;
}
}
I believe the problem is that when the database has just been created, there is nowhere for the moveToFirst() to go, as there is no data. But I am not sure how to get round this as I need a moveToFirst() for when there is a stocked database.
Anyone see a solution? Have a diagnosed the problem correctly?
You can check for an empty Cursor like this:
...
cursor = db.getAllRecords();
if(cursor.getCount() > 0)
DisplayRecords(cursor);
else
DisplayNoRecordsMessage();
Or since you posted DisplayRecords() in a previous question, you can also use:
...
if (c != null && !cursor.isAfterLast())
{
nameTxt.setText(c.getString(1));
phoneTxt.setText(c.getString(2));
emailTxt.setText(c.getString(3));
}
Also please read about Java naming convention which states that method names should start with a lowercase letter.
Cursor.moveToFirst() returns false in case of empty cursor.
See the doc: http://developer.android.com/reference/android/database/Cursor.html#moveToFirst()
I'd say the problem is in DisplayRecord().
Are you fetching data from the cursor in DisplayRecord() ? If so, inside of it you should check if the cursor contains some data, calling moveToFirst and checking its result for example.
Something like
private void DisplayRecord(Cursor c){
if(!c.moveToFirst()){
return;
}
// do stuff
}
Use if(cursor .moveToFirst()) to check cursor
public class MainActivity extends Activity {
DBAdaptor db;
Cursor cursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DBAdaptor(this).open();
cursor = db.getAllRecords();
if(cursor.moveToFirst()){
DisplayRecord(cursor);
}
}

How to save cursor in activity?

I'm population my listView using Cursor, but when I navigate away from my activity and then return back my listview is empty. Here is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
...
DBAdapter db = new DBAdapter(context);
db.open();
Cursor c = db.getAll();
db.close();
startManagingCursor(c);
String[] columns = new String[] { ... };
int[] to = new int[] { ... };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_item, c, columns, to);
this.setListAdapter(mAdapter);
...
}
I've seen here questions about saving position of Cursor, but not the Cursor itself. Probably I just missing something, shall I save my cursor (how can I do it?) or it's better(faster, cheaper) to create new cursor every time using my DBadapter?
Thanks
startManagingCursor() makes your close() call unnecessary. As long as you didnt get exceptions about not finalizing or closing your cursor you have done anything right.

Categories

Resources