Call SQLiteOpenHelper method from fragment

Shah Zeb

i want to call this method getAllData(); from Fragment, this method return cursor data from SQlite Database and i want to use that data inside my Fragment to create listView, i use these lines of code to access that data

PackagesDbHelper mydb = new PackagesDbHelper(getActivity());
Cursor result = mydb.getAllData();

but on app launch i get this error "unfortunately app has stopped" but if i exclude this line of code

Cursor result = mydb.getAllData();

my app works fine

PackagesDbHelper

public class PackagesDbHelper extends SQLiteOpenHelper {

public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor result = db.rawQuery("SELECT * FROM " + PackagesContract.ufoneEntry.TABLE_NAME, null);
    return result;
}

}

PackagesFragment

public class PackagesFragment extends Fragment {

private PackagesAdapter packagesAdapter;
PackagesDbHelper mydb = new PackagesDbHelper(getActivity());

@Override
public void onCreate(Bundle savedInstanceState) {
    Cursor result = mydb.getAllData();

    super.onCreate(savedInstanceState);
}


}
ρяσѕρєя K

Here:

PackagesDbHelper mydb = new PackagesDbHelper(getActivity());

line causing issue because calling getActivity() at class level will return invalid Activity Context.

Initialize mydb object inside onCreate or any other method :

mydb = new PackagesDbHelper(getActivity());
Cursor result = mydb.getAllData();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Call an activity method from a fragment

From Dev

Call method in fragment from another fragment/activity

From Dev

Call Fragment method from Activity

From Dev

Call a Fragment method from an Adapter

From Dev

How to call method inside the fragment from activity?

From Dev

Call fragment method from activity but view is null

From Dev

Call dynamic fragment method from activity

From Dev

failed to call Adapter method from fragment

From Dev

Call Tabbed Fragment method from Activity

From Dev

how to call method of one fragment from another fragment class in android

From Dev

Android - Trying to call parent method from fragment

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call Fragment Method from adapter class

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

Call method in fragment from another fragment/activity

From Dev

Call a method in a drawer fragment from the base activity

From Dev

Call Fragment method from Activity

From Dev

Call fragment method from activity but view is null

From Dev

failed to call Adapter method from fragment

From Dev

call method from fragment to fragment ( refresh adapter )

From Dev

Call method in child fragment in parent fragment from activity

From Dev

How call method from Fragment in Activity?

From Dev

How to call method from java class in fragment?

From Dev

How to call ViewHolder method from Activity/Fragment

From Dev

Call method in MainActivty from Fragment

From Dev

Android Call Fragment Method from Fragment

From Dev

How to call method from another fragment

From Dev

Call method from Main Activity into Fragment Adapter

From Dev

Call method of fragment B from Fragment A using Kotlin

Related Related

  1. 1

    Call an activity method from a fragment

  2. 2

    Call method in fragment from another fragment/activity

  3. 3

    Call Fragment method from Activity

  4. 4

    Call a Fragment method from an Adapter

  5. 5

    How to call method inside the fragment from activity?

  6. 6

    Call fragment method from activity but view is null

  7. 7

    Call dynamic fragment method from activity

  8. 8

    failed to call Adapter method from fragment

  9. 9

    Call Tabbed Fragment method from Activity

  10. 10

    how to call method of one fragment from another fragment class in android

  11. 11

    Android - Trying to call parent method from fragment

  12. 12

    Call method in child fragment in parent fragment from activity

  13. 13

    Call Fragment Method from adapter class

  14. 14

    (Android) how to call method in fragment from fragmentActivity

  15. 15

    Call method in fragment from another fragment/activity

  16. 16

    Call a method in a drawer fragment from the base activity

  17. 17

    Call Fragment method from Activity

  18. 18

    Call fragment method from activity but view is null

  19. 19

    failed to call Adapter method from fragment

  20. 20

    call method from fragment to fragment ( refresh adapter )

  21. 21

    Call method in child fragment in parent fragment from activity

  22. 22

    How call method from Fragment in Activity?

  23. 23

    How to call method from java class in fragment?

  24. 24

    How to call ViewHolder method from Activity/Fragment

  25. 25

    Call method in MainActivty from Fragment

  26. 26

    Android Call Fragment Method from Fragment

  27. 27

    How to call method from another fragment

  28. 28

    Call method from Main Activity into Fragment Adapter

  29. 29

    Call method of fragment B from Fragment A using Kotlin

HotTag

Archive