Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9231385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:03:14+00:00 2026-06-18T06:03:14+00:00

I am working for the first time with ListFragments. I want to display data

  • 0

I am working for the first time with ListFragments. I want to display data from a sqlite database.

My problem: I can’t retrieve the context.

I have tried this.getActivity() in my class which extends ListFragment- but the context is null.

Then I have tried to save the context of my first activity(the ui which is displayed after starting the app) of my application as a static variable. But is this the right context?

static Context context; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Setting the context
        context = this; 

        setContentView(R.layout.main);
    }

    public static Context getContext() {
        return context;
    } 

When I query the database I don’t get any exceptions but the cursor is always empty. I have looked into my sqlite database – I have three rows of data (so the cursor should not be empty).

My code for querying the data from the database:

Cursor cursor = null;
        try{
            open();
            cursor = database.rawQuery("select _id, surname, prename, street, zipcode, telephone, email from "+Database.getMyDbTable()+";", null);
        }catch(Exception ex){
            Log.e(TAG, "Could not get list");
            CharSequence text = "Die Liste kann nicht erstellt werden!";
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
        Log.d(TAG, "Cursor "+cursor); 
        close(); 

How do I get the right context?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:03:16+00:00Added an answer on June 18, 2026 at 6:03 am

    I found this tutorial, which was quite helpful (as I wanted to have a Tab Layout combined with a list):

    https://github.com/artem-zinnatullin/Android-List-Fragment-Tutorial

    I have modified the onCreate method:

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
            //Getting the data from the Database
            Context myContext = getActivity();           
            Log.d(TAG, "Context "+myContext);
    
            contactListManager = new ContactList(myContext);
            Log.d(TAG, "ContactListManager "+contactListManager);
    
            Cursor cursor =  contactListManager.fetchAllItems();
            Log.d(TAG, "Cursor "+cursor);
    

    with this code I can access my database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a simple application, first time working with SQLite. I want a regular
I am working first time on drupal and I have a little problem with
This is my first time working with a WPF datagrid. From what I understand
I am having an problem. First time working with a windows server, do you
im working with core-data for the first time, and my app is handling a
I'm working for the first time with images in a JFrame, and I have
So this is from a uni coursework and is my first time working with
I am working first time in SugarCRM I have really no idea regarding this.
First time working with DB2. I have a stored proc that performs a union,
This is my first time working with the Google Maps API. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.